0

I am trying to learn how to use sql data adapter ... I have coded the following to check how it works ...

the problem is i want to retrieve values of 3 columns(DeptNo,DeptId,DeptName) of my database table "Sana" separately and display them in three separate text boxes ...

Through the code mentioned below I am able to retrieve the value of entire tuple of data base table together

what should I do to reach above mentioned result???

    protected void Button1_Click(object sender, EventArgs e)
{

    SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Select DeptNo,DeptId,DeptName from Sana where DeptName='" + TextBox1.Text + "'", connect);
    SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
    DataSet MyDataSet = new DataSet();
    myAdapter.Fill(MyDataSet, "Departments");
    object[] rowVals = new object[3];

    foreach (DataTable myTable in MyDataSet.Tables)
    {
        foreach (DataRow myRow in myTable.Rows)
        {
            foreach (DataColumn myColumn in myTable.Columns)
            {
                Response.Write(myRow[myColumn] + "\t");

            }
        }
    }
}

}

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sana.91
  • 1,999
  • 4
  • 33
  • 52

1 Answers1

3
    foreach (DataRow myRow in MyDataSet.Tables[0].Rows)
    {
        TextBox1.Text = myRow["DeptNo"].ToString(); 
        TextBox2.Text = myRow["DeptId"].ToString(); 
        ... 
    }
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • At Wiktor: Following error appeared: **Error 1 foreach statement cannot operate on variables of type 'System.Data.DataTable' because 'System.Data.DataTable' does not contain a public definition for 'GetEnumerator'** – Sana.91 May 29 '12 at 19:48