Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

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\
Avatar of vs1784
vs1784

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;
    }
}
Avatar of Fernando Soto
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

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
yes i agree thats true.

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

Have a great day.
Fernando
Avatar of mathieu_cupryk

ASKER

Guys is thier a reason why you made it public?
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.  ;=)
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:             {
 
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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great job!