Solved

Need to convert  VB.Net code to C#

Posted on 2008-06-13
13
2,655 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
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 …

707 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

20 Experts available now in Live!

Get 1:1 Help Now