Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to retrieve student ID from database. Here is my code what i have tried but it is not displaying anything.



Thanks...

What I have tried:

protected void Button2_Click(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection("Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True");


if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
string str = "select * from StRecords where StID='" + Session["login"] + "'";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Label11.Text = dt.Rows[0]["StID"].ToString();

}


}
Posted
Updated 19-Sep-17 21:50pm
Comments
Prifti Constantine 20-Sep-17 2:39am    
You should just address the Datatable solely and say
Label11.Text= dt["StID"].ToString();
I dont think there is a need for a dataset in this situation... I hope this helps you out!
Karthik_Mahalingam 20-Sep-17 4:17am    
keep a break point and see what value you are getting in the session and try to run the query in sql studio and check whether it returns any data
Member 13361792 20-Sep-17 5:05am    
no session is not returning a value..
Can u plz help me how to do it
Animesh Datta 20-Sep-17 6:20am    
Check why the value of Session["login"] is empty or null. Put breakpoint and debug it.

1 solution

Try this

 DataTable dt = new DataTable();
            try
            {
                con = da_con.DA_Connect();
                SqlCommand cmd = new SqlCommand("FetchShopSellerOnId", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqlDa = new SqlDataAdapter();
                sqlDa.SelectCommand = cmd;
                sqlDa.Fill(dt);
<pre> foreach (DataRow dRow in dt.Rows)
                {
                    if (dt.Rows.Count > 0)
                    {
           Label11.Tex   = dRow["ShopOfficeDetailsId"].ToString();

                  }
}




In Above Code
dRow["ShopOfficeDetailsId"].ToString();
ShopOfficeDetailsID is used in stored procedure eg. Select ShopOfficeDetailsID ,, You can use your ID name
 
Share this answer
 
v2
Comments
Member 13361792 20-Sep-17 5:06am    
It doesn't work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900