same exact code works in vb.net but not in C#

can any help me why the same exact code works in vb.net but not in C# . case is very simple

I have a table called MenuItems which has menu items and a table sizeandprice which has price for each menuitem based on the size. simple case of 1 to many relationship between menuitems table to sizeandprice table. I am trying to display in a gridview control couple of fields from menuitems table and have another template field in which i am display price and size field from the child table which is sizeand price. so basically this is how my code looks like

Page in vb works fine

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then

Dim ds As SqlDataSource = CType(e.Row.FindControl("SqlDataSource2"), SqlDataSource)

ds.SelectParameters("fkMenuItemID").DefaultValue = GridView1.DataKeys(e.Row.RowIndex).Value

End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PPQ_DataConnectionString1 %>" SelectCommand="SELECT [MenuItemID], [MenuItemType], [ItemName] FROM [MenuItems]"></asp:SqlDataSource>

<br />

&nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MenuItemID"

DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">

<Columns>

<asp:BoundField DataField="MenuItemID" HeaderText="MenuItemID" InsertVisible="False"

ReadOnly="True" SortExpression="MenuItemID" />

<asp:BoundField DataField="MenuItemType" HeaderText="MenuItemType" SortExpression="MenuItemType" />

<asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />

<asp:TemplateField HeaderText="Size And Price">

<ItemTemplate>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:PPQ_DataConnectionString1 %>"

SelectCommand="SELECT [ItemSize], [ItemPrice] FROM [SizeAndPrice] WHERE ([fkMenuItemID] = @fkMenuItemID)">

<SelectParameters>

<asp:Parameter Name="fkMenuItemID" Type="Int32" />

</SelectParameters>

</asp:SqlDataSource>

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">

<ItemTemplate>

<%#Eval("ItemSize")%>: <%#Eval("ItemPrice", "$ {0:F2}")%><br />

</ItemTemplate>

</asp:Repeater>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

</form>

</body>

</html>

 

if you would notice that in order populate my repeater with childrows i need to know the menuitemid from the parent row which i do in rowdatabound event however same code in C# does not render any values for my item price and size defined inside the repeater control. here is how the c# page looks like

 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

SqlDataSource source2 = e.Row.FindControl("SqlDataSource2") as SqlDataSource;

source2.SelectParameters["fkMenuItemID"].DefaultValue = GridView1.DataKeys[e.Row.RowIndex].Value as string;

}

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PPQ_DataConnectionString1 %>"

SelectCommand="SELECT [MenuItemID], [MenuItemType], [ItemName] FROM [MenuItems]">

</asp:SqlDataSource>

</div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MenuItemID"

DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >

<Columns>

<asp:BoundField DataField="MenuItemID" HeaderText="MenuItemID" InsertVisible="False"

ReadOnly="True" SortExpression="MenuItemID" />

<asp:BoundField DataField="MenuItemType" HeaderText="MenuItemType" SortExpression="MenuItemType" />

<asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />

<asp:TemplateField HeaderText="Size And Price">

<ItemTemplate>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:PPQ_DataConnectionString1 %>"

SelectCommand="SELECT [ItemSize], [ItemPrice] FROM [SizeAndPrice] WHERE ([fkMenuItemID] = @fkMenuItemID)">

<SelectParameters>

<asp:Parameter Name="fkMenuItemID" Type="Int32" />

</SelectParameters>

</asp:SqlDataSource>

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">

<ItemTemplate>

<%#Eval("ItemSize")%>: <%#Eval("ItemPrice", "$ {0:F2}")%><br />

</ItemTemplate>

</asp:Repeater>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</form>

</body>

</html>

 

can anyone tell me what i am doing wrong?

0
zeeshanhirani
2/9/2006 6:55:02 AM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

1 Replies
765 Views

Similar Articles

[PageSpeed] 42

Just by inspection and without testing I see two errors.

SqlDataSource source2 = e.Row.FindControl("SqlDataSource2") as SqlDataSource;

needs to be:

SqlDataSource source2 = (SqlDataSource)e.Row.FindControl("SqlDataSource2");

and

source2.SelectParameters["fkMenuItemID"].DefaultValue = GridView1.DataKeys[e.Row.RowIndex].Value as string;

should be:

source2.SelectParameters["fkMenuItemID"].DefaultValue = GridView1.DataKeys[e.Row.RowIndex].Value;

Hope that helps.

0
awoomer
2/14/2006 3:22:39 PM
Reply:

Similar Artilces:

use VB.NET and C#.NET code in the same C#.NET project
All-- Here is a sample that is "off the beaten path", (at least for me). Is it possible, in an ASP.NET application, using the code-behind page building technique, to have both pages written in VB.NET and pages written C#.NET?At http://www.WebLogicArts.com/DemoList.aspx there is a sample that shows that, (contrary to popular belief), it IS possible to mix ASP.NET pages built with C#.NET with ASP.NET pages built with VB.NET in the same VS.NET 2003 project. Note that this is just a "fun" sample to see if it can be done and I do not recommend this practice as a "standard" way of develo...

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

Type.GetType does not work in VB.NET but works in C#. VB.NET gurus Please help
Friends,   I am an experienced C# programmer who is working on a VB.NET project now. I am writing different methods covering the following functionalities 1) Take a datareader as input and return an arraylist of class object2) Take an xmlnode (received from a webservice) as input and return an arraylist of class object. The methods are generic methods which take datareader/xmlnode as first parameter and classname (string) as the second parameter. This way it will work trivially. The schema of class object matches with the input (datareader or xmlnode)In C# I used to do th...

Translate c#.net code to vb.net code
Hi I can usually do the translation myself, but this time I am a bit stumped  Let us consider to rewrite Urls (more than 16000 links) using a table. Save original url and rewritten url in a table. Now add a class file in App_Code folder. class UrlRewriteModule : IHttpModule{    public void Dispose()    {        // Nothing to dispose    }    public void Init(HttpApplication context)    {        context.BeginRequest += new EventHandler(context...

How to convert C#.Net code to VB.Net code ???...
HI, i was searching something new, then i suddenly find some tips to convert C#.Net code to VB.Net but i did not do well. i am puttint its url here plz tell me how to convert C#.Net code to VB.Net. http://www.dotnetspider.com/code/C-273-Convert-C-VB-NET.aspx Jasim...Please remember to click “Mark as Answer” on the post that helps youJasim AkhtarNew Delhi ( INDIA ) It would be better if you write the problem you having, while converting from C# to VB.NET. You cannot completely rely on online converters. ThanksMark post(s) as "Answer" that helped youElectronic ScrewWebsite||Bl...

How to change vb.net code to C#.Net ?...
Hi, Earlier i had a class  that i got it from this Forum, it in vb.net and i am trying it to convert it in c#.net but i am unable to covert properly to work. the functionality of this class is to convert number value to words. i am putting both of code c#.net and VB.net please rectify it.Public Class NumberToWords Dim Character As String Public Function ConvertNumber(ByVal A As Double) As String On Error Resume Next Dim MM Dim IM ConvertNumber = "" MM = Microsoft.VisualBasic.Len(A) IM = Microsoft.VisualBasic.Left(A, Mic...

How to migrate code from vb.net to c#.net?
Hi all,If anybody knows information on migration of VB.Net application to C#.net. i.e Any tools available, best practices, methodology for estimation. etc.   RegardsHelen    Hey, Take a look at this topic http://forums.asp.net/t/1100603.aspxMaybe some of these sites can be of help to you.  Arnold you can do it by page by page just copy and paste ur page in the following link http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx and convert ur vb.net to c# gotcha!!YasserSpica.www.spica.aewww.bncnetwork.netPlease remember to mark as answers i...

Migrate code from vb.net to C#.net
Hello friends, I had created a web site using visual studio 2005. web site was created using the language vb i.e. VB.net. Now as per the clint requirement I have to migrate all the code in C#. So the question is that is there any tool or any inbuild utility in Visual Stuidio so that we can migrate the site from vb.net to C#.net. Or we have to do it mannually i.e. by changing each and every line of code mannually? Kindly suggest me. Thanks & Regards Girish NehteThanks & RegardsGirish NehteIf This post helps you to solve your problem please mark this as an answer. Hi, at the mome...

vb.net code works but not c# code
Hello, I'm trying to insert the metatags dynamically. The following vb code works fine: aspx.vb code: Protected myTag As System.Web.UI.HtmlControls.HtmlGenericControl Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myTag.Attributes("content") = "test" End Sub In aspx file head: <meta id="myTag" runat="server" /> The above code is working great. But the same thing I'm trying to put in C# which is not working. aspx.cs code: ...

converting to vb.net from c#.net authorize.net
authorize.net offered me some sample code when I signed up with them the only problem is the sample code is in c#.net but my page that they type all of their credit card into is vb.net <code><%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.IO" %> <script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E) { myPage.Text = readHtmlPage("https://certification.authorize.net/gateway/transact.dll"); } private String readHtmlPage(string url) { ...

vb.net vs c#.net vs j#.net
just out of curiousity which looks, acts most like java? i'm still learning trying to used to vb.net but it seems that it was easier to do some stuff with java. C# is Java++ in my opinion. If you have done C or Jave you will prefer C#.Mike Schellenberger - MCAD If you are looking for language similar to java go for J#. However most code samples you find on the net will be in VB.NET and C#....

VB.Net To C#.Net
Hello... Since Last from 1 year i had develope web application in using vb.net now i want to transfer from vb.net to c#.net so what should i do to transform to c# language any link or book related to me through i can easily understand and coding using c# in a short time... Thanks~ Mark As Answer If UseFull ~Kaushal. Hi, you can read the specifications of the language. You're already familiar with .NET and how to program in an OO manner thanks to your experience with vb.net. The only thing you need to learn is the new syntax of C#. You can also convert parts of your v...

vb .net or c# .net
I currently develop websites using php but have been requested to develop a site using asp.net. The question I have is for someone who knows php would it be better to transition to vb.net or c#.net? Thanks, Fr. Robert C# -> There is a converter that will transition about 60% or more of your PHP code to C#. Plus C# is closer to PHP than VB.NET.Jason N. GaylordASPInsider and Microsoft MVPhttp://jasongaylord.com...

C#.NET or VB.NET
what lang does everyone prefer? Thanks, Peter Iuvara, MCP It's a very famous debatable question in forums! I would say if you are from Java/C/C++, then C# would be easy to use for you. If you are from ASP/VB, then VB.NET would be easy for you! As long as if you follow CLS, then what ever the language you use, it's going to be the same for CLR!Sreedharhttp://www.w3coder.orgweblog http://weblogs.asp.net/skoganti I would agree with that ... just curious as to what user's on this forum predominantly code in?Thanks, Peter Iuvara, MCP I came from no real programming background, started i...

Web resources about - same exact code works in vb.net but not in C# - asp.net.sql-datasource

Trump Calls Saudi Prince 'Dopey'
Daily Beast Trump Calls Saudi Prince 'Dopey' Daily Beast Republican presidential hopeful Donald Trump is in yet another Twitter fight, this ...

Paris Climate Talks Called a 'Fraud' By the Father of Climate Science as Nations Make a Deal
As the climate negotiations in Paris —the ones that are pretty much the last-ditch effort to save the world as we know it—draw to a close, scientists ...

Authorities: Fire at Southern California mosque was arson
CBS News Authorities: Fire at Southern California mosque was arson Sacramento Bee A Southern California mosque was damaged in a fire that ...

LOL: ‘Star Wars: The Force Awakens’ Gets a New Holiday Special
Any Star Wars fan can tell you that even though plenty of people don’t like the prequels, the worst thing to come out of the sci-fi saga is undoubtedly ...

Barack Obama: Discrimination Against Muslims In The United States Only Strengthens ISIS
President Barack Obama is taking a stand for Muslims, warning Americans that stereotyping all Muslim people based on the actions of violent extremists ...

Police: Teen who pointed BB gun at officer shot in leg
NBCNews.com Police: Teen who pointed BB gun at officer shot in leg seattlepi.com JACKSONVILLE, Fla. (AP) — A black teenager carrying a BB ...

Elon Musk Backs $1 Billion Artificial Intelligence Research Group
Elon Musk and other technology giants have chipped in $1 billion toward researching artificial intelligence to “benefit humanity,” the business ...

Here's how the top iPhone apps are using 3D Touch
When Apple introduced 3D Touch on the iPhone 6S this September, it described the technology as a revolutionary paradigm for interacting with ...

Banksy draws attention to refugee crisis with Steve Jobs mural - Videos - CBS News
Street artist Banksy is weighing in on the Syrian refugee crisis with a series of murals in the French port of Calais, including an image of ...

Has Donald Trump Been Betrayed By His Own Party?
Donald Trump’s bid for the 2016 Republican presidential nomination has been controversial to say the least, but it cannot be argued that Donald ...

Resources last updated: 12/12/2015 5:47:07 PM