Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to convert  VB.Net code to C#

Posted on 2008-06-13
13
Medium Priority
?
2,696 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
Introduction to Processes

688 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