Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This Is My code:"Plz Provide Me Solution"
C#
protected void btnsubmit_Click(object sender, EventArgs e)
{
 try
   {
   //Retrieving user id.
cmd = new SqlCommand("select nuser_id from tbl_User_Info where suser_login = '"+ListBox1.SelectedValue+"'", con);
   DataSet ds10 = new DataSet();
   DataTable dt10 = new DataTable();
   SqlDataAdapter da10 = new SqlDataAdapter(cmd);
   da10.Fill(ds10);
   string uId = ds10.Tables[0].Rows[0]["nuser_id"].ToString();
   int userId = Convert.ToInt32(uId);
   }
  catch (Exception ex)
  {
  }       
}
Posted
Updated 18-Aug-15 20:50pm
v3

use parameter and ExecuteScalar
C#
cmd = new SqlCommand("select nuser_id from tbl_User_Info where suser_login =@Para1", con);
cmd.Parameters.AddWithValue("@Para1", ListBox1.SelectedValue);
int? id = (int?)cmd.ExecuteScalar();
if(id.HasValue)
{
  // there is value for ID, use id.Value to get the value 
}
 
Share this answer
 
Comments
Sanket Saxena 19-Aug-15 6:50am    
simple and perfect +5
DamithSL 19-Aug-15 10:55am    
Thank you
Try this...

C#
protected void btnsubmit_Click(object sender, EventArgs e)
{
 try
   {
   //Retrieving user id.
cmd = new SqlCommand("select nuser_id from tbl_User_Info where suser_login = '"+ListBox1.SelectedValue+"'", con);
   DataSet ds10 = new DataSet();
   DataTable dt10 = new DataTable();
   SqlDataAdapter da10 = new SqlDataAdapter(cmd);
   da10.Fill(ds10);
   if(ds10.Tables[0].Rows.Count >0)
   {
   string uId = ds10.Tables[0].Rows[0]["nuser_id"].ToString();
   int userId = Convert.ToInt32(uId);
   }
   }
  catch (Exception ex)
  {
  }       
}
 
Share this answer
 

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