Skip to content

How to get field name

bdesh tiger

Asked Jun 5, 2007, 1:15 PM EDT4 answers
Share
LinkedInX
Dear All,

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

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    C#
    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"];
    0
  • 0
    Hi Mike,

    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.Text

    in the place of how_to_get_column_name i need to know columnname.

    0
  • 0

    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 + "'";

    0
  • 0
    Hi Richards,

    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

    0