How to get column values ​​separately via sql data adapter class?

I am trying to find out how to use the sql data adapter ... I have coded the following to test how it works ...

the problem is I want to get the values ​​from 3 columns (DeptNo, DeptId, DeptName) of my Sana database table separately and display them in 3 separate text boxes ...

In the below code, I can get the value of a whole set of database table together

what should I do to achieve the above 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");

            }
        }
    }
}

      

}

0


source to share


1 answer


    foreach (DataRow myRow in MyDataSet.Tables[0].Rows)
    {
        TextBox1.Text = myRow["DeptNo"].ToString(); 
        TextBox2.Text = myRow["DeptId"].ToString(); 
        ... 
    }

      



+2


source







All Articles