Convert vb.net to c#

Public Function GetCountryName(ByVal ID As String) As String
        Dim ds As New DataSet
        ds.ReadXml("C:\Path\FileName.xml")
        Dim drs() As DataRow = ds.Tables("countries").Select("countryid = '" & ID & "'")
        If drs.Length <> 0 Then
            Return drs(0)("name").ToString
        Else
            Return String.Empty
        End If
    End Function
to
I have errors:
    private string GetCountryName(string ID)
        {

            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("/GeneralInformation/Countries.xml"));          
            DataRow dr = new DataRow();
            dr = ds.Tables["countries"].Select["countryid = '" + ID + "'"];
            if (ds.Length != 0)
            {
                return dr[0]["name"].ToString();
            }
            else
            {
                return null;
            }

        }
================================================================================
Error      1      'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level      D:\Inetpub\wwwroot\omegalove\Default.aspx.cs      46      26      D:\...\omegalove\
Error      2      Cannot apply indexing with [] to an expression of type 'method group'      D:\Inetpub\wwwroot\omegalove\Default.aspx.cs      47      18      D:\...\omegalove\
Error      3      'System.Data.DataSet' does not contain a definition for 'Length'      D:\Inetpub\wwwroot\omegalove\Default.aspx.cs      48      20      D:\...\omegalove\
Error      4      Cannot apply indexing with [] to an expression of type 'object'      D:\Inetpub\wwwroot\omegalove\Default.aspx.cs      50      24      D:\...\omegalove\
mathieu_cuprykAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vs1784Commented:
public string GetCountryName(string ID)
{
    DataSet ds = new DataSet();
    ds.ReadXml("C:\\Path\\FileName.xml");
    DataRow[] drs = ds.Tables("countries").Select("countryid = '" + ID + "'");
    if (drs.Length != 0) {
        return drs(0)("name").ToString;
    }
    else {
        return string.Empty;
    }
}
0
Fernando SotoRetiredCommented:
Hi mathieu_cupryk;

This should be it.

Fernando
public string GetCountryName(string ID)
{
    DataSet ds = new DataSet();
    ds.ReadXml(@"C:\Path\FileName.xml");
    DataRow[] drs = ds.Tables["countries"].Select("countryid = '" + ID + "'");
    if( drs.Length != 0 )
    {
        return drs[0]["name"].ToString();
    }
    else
    {
        return String.Empty;
    }
}

Open in new window

0
Fernando SotoRetiredCommented:
Hi vs1784;

You almost had it. In C# we use [ ] for arrays so this line in your sample:
   
    ds.Tables("countries").Select("countryid = '" + ID + "'");
    return drs(0)("name").ToString;

Should be :

    ds.Tables["countries"].Select("countryid = '" + ID + "'");
    return drs[0]["name"].ToString();

Fernando
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

vs1784Commented:
yes i agree thats true.

Thanks
0
Fernando SotoRetiredCommented:
It is difficult when working with multiple languages, it happens to all of us.

Have a great day.
Fernando
0
mathieu_cuprykAuthor Commented:
Guys is thier a reason why you made it public?
0
Fernando SotoRetiredCommented:
Sure there is. Your question was to convert the VB code to C#. The original VB code was Public therefore we made the C# code public. But you can change it to private if that is what you want.  ;=)
0
mathieu_cuprykAuthor Commented:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 44:             ds.ReadXml(Server.MapPath("GeneralInformation/Countries.xml"));
Line 45:            
Line 46:             DataRow[] drs = ds.Tables["countries"].Select("countryid = '" + ID + "'");
Line 47:             if (drs.Length != 0)
Line 48:             {
 
0
Fernando SotoRetiredCommented:
Hi mathieu_cupryk;

I tested the code and it worked for me. Not knowing which of the three line the exception happened on in your code and assuming that the file  GeneralInformation/Countries.xml exists the only other line that could cause this type of error is this line:

    DataRow[] drs = ds.Tables["countries"].Select("countryid = '" + ID + "'");

Now we know that ds is a valid object cause you created an instance of it 2 lines up in the code. The only thing left is either the table is not created or the table was created but the table name you are using is not the same as what was created.

If you post a sample of the Xml file with the Attach File option on this page I will test it with your data.

Fernando
0
Fernando SotoRetiredCommented:
Hi mathieu_cupryk;

With the information posted to the other question, http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23150982.html my assumption was correct in that the reason for the System.NullReferenceException was due to the name of the Table. The ds.ReadXml method calls the table country as opposed to the name you used "countries".

Fernando
public string GetCountryName(string ID)
{
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("GeneralInformation/Countries.xml"));
    DataRow[] drs = ds.Tables["country"].Select("countryid = '" + ID + "'");
    if (drs.Length != 0)
    {
        return drs[0]["name"].ToString();
    }
    else
    {
        return String.Empty;
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mathieu_cuprykAuthor Commented:
Great job!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.