2
Reply

C# asp.net winforms strings

hisham kal

hisham kal

Dec 15 2013 7:12 AM
1k

Am trying to read product name from the sql database and iwant to display it using amessage box on the form.but when i debug,the message box is empty.where is the problem.

//code to read name from the database.
 public string hardname()  
{
string name = "";
try  
{                
SqlConnection con = new SqlConnection("Data Source=HISHAMKAL-PC;Initial Catalog=LA GHardware;User ID=sa;Password=hish;");                
SqlCommand cmd = new SqlCommand("SELECT 'Product_Name' FROM Hardware WHERE ID=" + textBox1.Text,con);  
SqlDataReader reder = cmd.ExecuteReader();                
while (reder.Read())  

name =(string)reder[0];
}  
con.Close();           
}                
catch(Exception x)            
{                    
 x.ToString();                
}            
return name;
          
  }       
//code behind the button
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(hardname());

          }


Answers (2)