I have a table that contains information like(examId, examActive) and I want to display a particular examId in a textbox. I am new
in ASP.Net and SQL. Please Help me. Here is my code, what
else I have to do.
vcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select examId from ExamConfig where examActive='1'", vcon);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(ds);
txtExamId.Text = dt.Rows[1]["examId"].ToString();
vcon.Close();
Loading

Riyaz AkhtarRank #2,275 · 619 reputationPosted Jul 17, 2013, 12:42 AM EDT
Anurag SarkarRank #1,049 · 1.8K reputationPosted Jul 17, 2013, 12:47 AM EDT
SqlConnection con = new SqlConnection("Data Source=hightech;Initial Catalog=session; User ID=sa; password=sa");
String str = "select ename,job from emp";
SqlCommand cmd = new SqlCommand(str, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = dr["ename"].ToString();
Label1.Text = dr["job"].ToString();
}
con.Close();
Thanx
Sayed Hassan17 reputationPosted Jul 17, 2013, 12:49 AM EDT
Riyaz AkhtarRank #2,275 · 619 reputationPosted Jul 17, 2013, 12:53 AM EDT
txtExamId.Text = ds.Tables[0].Rows[0]["examId"].ToString();
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 17, 2013, 12:56 AM EDT
SqlDataAdapter da = new SqlDataAdapter("select examId from ExamConfig where examActive='1'", vcon);
DataSet ds = new DataSet();
da.Fill(ds);
txtExamId.Text =ds.Tables[0].Rows[0][0].ToString();
Kunal VaishyaRank #990 · 2K reputationPosted Jul 17, 2013, 1:23 AM EDT
Ravi ShekharRank #673 · 3.6K reputationPosted Jul 17, 2013, 1:47 AM EDT
SqlDataAdapter da = new SqlDataAdapter("select examId from ExamConfig where examActive='1'", vcon);
DataTable dt = new DataTable();
da.Fill(dt);
txtExamId.Text = dt.Rows[0]["examId"].ToString();
==============================================
Now, If your query returns multiple row then you have to take any data container like datagridview, datalist etc. and do coding like below...
SqlDataAdapter da = new SqlDataAdapter("select examId from ExamConfig where examActive='1'", vcon);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
datagridview1.datasource=dt;
datagridview1.databind();
}
Regards,
ravi
mark it as answer if it helped you