hisham kal

hisham kal

  • NA
  • 1
  • 1.5k

C# asp.net winforms strings

Dec 15 2013 7:12 AM

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)