Jumed from VB to C# thus getting deficulty on every step. How to get fieldname from a table?
cnn = new OleDbConnection(conStr);
cnn.Open();
da = new OleDbDataAdapter("select * from " + sTableName, cnn);
ds = new DataSet();
da.Fill(ds, sTableName);
It works till here. Now to save data i want to get field name
lssql = "update " + tablename + " set " + fieldName + "='" +
txtDescription.Text + "'";
savedata codes goes here....
In my table i have two field like ID and description. How can i get 2nd field name by coding?
I am using VB2005, C#, OleDb command
Thanks in advantage.
BT
bdesh tigerPosted Jun 6, 2007, 1:30 AM
Thank you so much. Actually i was looking for the below command which you mentioned as follows:
ds.Tables[0].Columns[2].Caption (this will give me the 2nd column name)
It saved my many lines extra coding.
Thanks a lot.
BT
richard limPosted Jun 5, 2007, 10:25 PM
Hi maybe you can specify inside the query like this.
da = new OleDbDataAdapter("select id, description, etc. from " + sTableName, cnn);
ds = new DataSet();
da.Fill(ds, sTableName);
now you can specify the fieldname like this.
lssql = "update " + tablename + " set description ='" +
txtDescription.Text + "'";
Or in dataset.
lssql = "update " + tablename + " set " + ds.Tables[0].Columns[2].Caption + "='" +
txtDescription.Text + "'";
Or if you have a datagridview you can use this code to get the fieldname.
lssql = "update " + tablename + " set " + datagridview1.Columns[2].HeaderText + "='" +
txtDescription.Text + "'";
bdesh tigerPosted Jun 5, 2007, 5:54 PM
Thank you for your fast response.
Actually i want to know the field name by coding. I have five tables like
Table 1 - ID, Nationality
Table 2 - ID, Department
Table 3 - ID, Customertype
.....
I want to use one form to save data for the five tables.
I have dificulty to get fieldname below:
lssql =
"update " + tablename + " set " + how_to_get_column_name + "='" + txtDescription.Textin the place of how_to_get_column_name i need to know columnname.
Mike GoldPosted Jun 5, 2007, 5:21 PM
if ds is your DataSet, this is how you get data from the first row, (this assumes your id is an int and your description is a string).
DataRow dr = ds.Tables[0].Rows[0];
int id = (int)dr["ID"];
string name = (string)dr["description"];