Need to convert VB.Net code to C#

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"))
            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!
Who is Participating?
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
try with:
TitleTextBox.Text = DS.Tables[0].Rows[0]["Title"];

    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"));
        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");

AnushartAuthor Commented:
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 ..
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Does your VB code work?

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

Jaime OlivaresSoftware ArchitectCommented:
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.
Good catch!  Looks like I need to find a new converter tool...  :)

Jaime OlivaresSoftware ArchitectCommented:
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.
Right, I know C#.  It's just been a while...

AnushartAuthor Commented:
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#
AnushartAuthor Commented:
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"];

TitleTextBox.Text = DS.Tables[0].Rows[0]["Title"].ToString();

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]);

AnushartAuthor Commented:
TitleTextBox.Text = (string)DS.Tables[0].Rows[0]["Title"];
Solved this
Thank you!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.