Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to convert  VB.Net code to C#

Posted on 2008-06-13
13
Medium Priority
?
2,699 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

877 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