mathieu_cupryk
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\FileNa me.xml")
Dim drs() As DataRow = ds.Tables("countries").Sel ect("count ryid = '" & 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( "/GeneralI nformation /Countries .xml"));
DataRow dr = new DataRow();
dr = ds.Tables["countries"].Sel ect["count ryid = '" + ID + "'"];
if (ds.Length != 0)
{
return dr[0]["name"].ToString();
}
else
{
return null;
}
}
========================== ========== ========== ========== ========== ========== ====
Error 1 'System.Data.DataRow.DataR ow(System. Data.DataR owBuilder) ' is inaccessible due to its protection level D:\Inetpub\wwwroot\omegalo ve\Default .aspx.cs 46 26 D:\...\omegalove\
Error 2 Cannot apply indexing with [] to an expression of type 'method group' D:\Inetpub\wwwroot\omegalo ve\Default .aspx.cs 47 18 D:\...\omegalove\
Error 3 'System.Data.DataSet' does not contain a definition for 'Length' D:\Inetpub\wwwroot\omegalo ve\Default .aspx.cs 48 20 D:\...\omegalove\
Error 4 Cannot apply indexing with [] to an expression of type 'object' D:\Inetpub\wwwroot\omegalo ve\Default .aspx.cs 50 24 D:\...\omegalove\
Dim ds As New DataSet
ds.ReadXml("C:\Path\FileNa
Dim drs() As DataRow = ds.Tables("countries").Sel
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(
DataRow dr = new DataRow();
dr = ds.Tables["countries"].Sel
if (ds.Length != 0)
{
return dr[0]["name"].ToString();
}
else
{
return null;
}
}
==========================
Error 1 'System.Data.DataRow.DataR
Error 2 Cannot apply indexing with [] to an expression of type 'method group' D:\Inetpub\wwwroot\omegalo
Error 3 'System.Data.DataSet' does not contain a definition for 'Length' D:\Inetpub\wwwroot\omegalo
Error 4 Cannot apply indexing with [] to an expression of type 'object' D:\Inetpub\wwwroot\omegalo
Hi mathieu_cupryk;
This should be it.
Fernando
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;
}
}
Hi vs1784;
You almost had it. In C# we use [ ] for arrays so this line in your sample:
ds.Tables("countries").Sel ect("count ryid = '" + ID + "'");
return drs(0)("name").ToString;
Should be :
ds.Tables["countries"].Sel ect("count ryid = '" + ID + "'");
return drs[0]["name"].ToString();
Fernando
You almost had it. In C# we use [ ] for arrays so this line in your sample:
ds.Tables("countries").Sel
return drs(0)("name").ToString;
Should be :
ds.Tables["countries"].Sel
return drs[0]["name"].ToString();
Fernando
yes i agree thats true.
Thanks
Thanks
It is difficult when working with multiple languages, it happens to all of us.
Have a great day.
Fernando
Have a great day.
Fernando
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. ;=)
ASKER
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.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 44: ds.ReadXml(Server.MapPath( "GeneralIn formation/ Countries. xml"));
Line 45:
Line 46: DataRow[] drs = ds.Tables["countries"].Sel ect("count ryid = '" + ID + "'");
Line 47: if (drs.Length != 0)
Line 48: {
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.NullReferenceExcept
Source Error:
Line 44: ds.ReadXml(Server.MapPath(
Line 45:
Line 46: DataRow[] drs = ds.Tables["countries"].Sel
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/Countri es.xml exists the only other line that could cause this type of error is this line:
DataRow[] drs = ds.Tables["countries"].Sel ect("count ryid = '" + 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
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/Countri
DataRow[] drs = ds.Tables["countries"].Sel
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great job!
{
DataSet ds = new DataSet();
ds.ReadXml("C:\\Path\\File
DataRow[] drs = ds.Tables("countries").Sel
if (drs.Length != 0) {
return drs(0)("name").ToString;
}
else {
return string.Empty;
}
}