Solved

Need to convert  VB.Net code to C#

Posted on 2008-06-13
13
2,666 Views
Last Modified: 2013-12-17
Here is my VB.Net code
......................................................
 If Trim(Request.QueryString("id")) <> "" And Not (IsPostBack) Then
            'create sql statement to get record by "id"
             Dim DS As New DataSet
            Dim SqlString As String
            SqlString = "Select date,title,Description,Author,Checker from TBL_PRESS_RELEASE where ID=" & (Request.QueryString("id")        
    Dim DataAdapter1 As New SqlDataAdapter(SqlString, ConfigurationSettings.AppSettings("ConnStr"))
            DataAdapter1.Fill(DS)
            TitleTextBox.Text = DS.Tables(0).Rows(0).Item("Title")
            DateTextBox.Text = DS.Tables(0).Rows(0).Item("Date")
            AuthorDropDownList.Items(0).Value = DS.Tables(0).Rows(0).Item("Author")
            FreeTextBox1.Text = DS.Tables(0).Rows(0).Item("Description")
         End If
Let me know if I need to declare any built in classes on top of my page
Thank You!
0
Comment
Question by:Anushart
  • 6
  • 4
  • 3
13 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 21780136
Untested:

{
    if (Strings.Trim(Request.QueryString("id")) != "" & !(IsPostBack)) {
       
        //create sql statement to get record by "id"
        DataSet DS = new DataSet();
        string SqlString;
        SqlString = "Select date,title,Description,Author,Checker from TBL_PRESS_RELEASE where ID=" + Request.QueryString("id");
        SqlDataAdapter DataAdapter1 = new SqlDataAdapter(SqlString, ConfigurationSettings.AppSettings("ConnStr"));
        DataAdapter1.Fill(DS);
        TitleTextBox.Text = DS.Tables(0).Rows(0).Item("Title");
        DateTextBox.Text = DS.Tables(0).Rows(0).Item("Date");
        AuthorDropDownList.Items(0).Value = DS.Tables(0).Rows(0).Item("Author");
        FreeTextBox1.Text = DS.Tables(0).Rows(0).Item("Description");
    }
}

0
 

Author Comment

by:Anushart
ID: 21780246
Error      1      'System.Linq.Strings' is inaccessible due to its protection level      
Error      2      Non-invocable member 'System.Web.HttpRequest.QueryString' cannot be   used like a method.      
Error      3      Non-invocable member 'System.Web.HttpRequest.QueryString' cannot be used like a method.      
Error      4      Non-invocable member 'System.Configuration.ConfigurationSettings.AppSettings' cannot be used like a method.
Error      5      Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method.      
Error      6      Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method.      
Error      7      Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method.      


I'm getting the above errors...Can you help me out ..
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 21780580
Does your VB code work?

What kind of classes are you declaring at the top using the "Imports" statement?

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21780605
notice that in C#, collecction indexers are used with brackets [], not with parenthesis ()
So, this line:
TitleTextBox.Text = DS.Tables(0).Rows(0).Item("Title")

should be:
TitleTextBox.Text = DS.Tables[0].Rows[0].Item["Title"]

similar for other lines.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 21780625
Good catch!  Looks like I need to find a new converter tool...  :)

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21780719
unfortunately, converters are not so smart to determine if a parenthesis in vb is refered to a method or an indexer.
you will have to do it manually. But it is relatively easy, just follow the error messages.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 27

Expert Comment

by:VBRocks
ID: 21780764
Right, I know C#.  It's just been a while...

0
 

Author Comment

by:Anushart
ID: 21780886
Thank You ...but  this
TitleTextBox.Text = DS.Tables[0].Rows[0].Item["Title"]
is giving another error as Sysytem.Data.Datarow doesnot contain a definition for item..
Even the intellisense doesnt show item after DS.Tables[0].Rows[0].
Can you let me know how to access  item in table using C#
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 21780918
try with:
TitleTextBox.Text = DS.Tables[0].Rows[0]["Title"];
0
 

Author Comment

by:Anushart
ID: 21781100
Error      1      Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)      

this is the error I get  at TitleTextBox.Text = DS.Tables[0].Rows[0]["Title"];

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 21781199
TitleTextBox.Text = DS.Tables[0].Rows[0]["Title"].ToString();

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 21781256
Anushart:  C# is REALLY picky about things being perfect, or "spelled out" if you will...  Every "i" has to
be dotted and every "t" has to be crossed.

Because a DataColumn in a DataTable can be any DataType (String, Int32, Boolean, etc), you have
to explicitly "cast" it to it's correct type.  Casting can be performed using the Convert class:

3 examples:

                string s= Convert.ToString(row[0]);
                int32 i = Convert.ToInt32 (row[0]);
                bool b = Convert.ToBoolean(row[0]);

0
 

Author Comment

by:Anushart
ID: 21781258
TitleTextBox.Text = (string)DS.Tables[0].Rows[0]["Title"];
Solved this
Thank you!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now