How to get names of Columns without datatype?  
Author Message
error1408





PostPosted: Visual C# General, How to get names of Columns without datatype? Top

Hi,

i want to get the columnname of an dataset, but without the datatype attached. The dataset is filled by the readxml-method and the columnname looks like this: defaultUser_text
My xml-file contains this: <defaultUser>admin</defaultUser>

Is there a way to rip off the _text or do i have to cut it the dirty way, manually

Thank you.



Visual C#10  
 
 
ahmedilyas





PostPosted: Visual C# General, How to get names of Columns without datatype? Top

hmm. Why dont you load the xml into the dataset then get the the column name

theDataSet.Tables[0].Columns[index].ColumnName;

would this not work for you



 
 
Mousy





PostPosted: Visual C# General, How to get names of Columns without datatype? Top

Ya, you can simple call the column name that way. In order to get rid of that u can use the "String.split" method to split the back part off and take the front one.

string Splitter=DataSet.Tables[0].Rows[index].ColumnName.split('_');

Splitter[0] -> defaultUser

Splitter[1] -> text

its better not to rip off the text this way, in future maybe u need some validation of checking on the type u can still used the "text" as a sense of identification on coding.


 
 
ahmedilyas





PostPosted: Visual C# General, How to get names of Columns without datatype? Top

you should try to avoid doing string parsing wherever possible as its ****/dirty and not always reliable

 
 
error1408





PostPosted: Visual C# General, How to get names of Columns without datatype? Top

That's exact my problem, i do it THIS way :) and i don't know how to kill that _text without parsing and splitting.

public Dictionary<string, string> getSettings()

{

DataSet dsSettings = new DataSet("dsSettings");

try

{

dsSettings.ReadXml(fullPath);

}

catch (Exception)

{

return null;

}

Dictionary<string, string> dicSettings = new Dictionary<string, string>();

foreach (DataTable table in dsSettings.Tables)

{

foreach (DataRow row in table.Rows)

{

foreach (DataColumn col in table.Columns)

{

dicSettings[col.ColumnName] = row[col].ToString();

}

}

}

return dicSettings;

}