Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 2.02 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Data can't be inserted in database
  2. protected void Button2_Click(object sender, EventArgs e)
  3. {
  4.     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
  5.     SqlCommand cmd = new SqlCommand("Insert into CarTab(Brand,Model,Plate,Color,Service) Values (@brand,@model,@plate,@color,@year,@service)",conn);
  6.  
  7.     cmd.CommandType = CommandType.Text;
  8.     cmd.Parameters.AddWithValue("@brand", Label1.Text);
  9.     cmd.Parameters.AddWithValue("@model", Label2.Text);
  10.     cmd.Parameters.AddWithValue("@plate", Label3.Text);
  11.     cmd.Parameters.AddWithValue("@color", Label4.Text);
  12.     cmd.Parameters.AddWithValue("@year", Label5.Text);
  13.     cmd.Parameters.AddWithValue("@service", Label6.Text);
  14.  
  15.     conn.Open();
  16.     cmd.ExecuteNonQuery();
  17. }
  18.        
  19. SqlCommand cmd = new SqlCommand(
  20. "Insert into CarTab(Brand,Model,Plate,Color,Year,Service)
  21.     Values (@brand,@model,@plate,@color,@year,@service)",
  22. conn);
  23.        
  24. using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
  25. {
  26.     using(SqlCommand cmd = new SqlCommand("Insert into CarTab(Brand,Model,Plate,Color,Service) Values (@brand,@model,@plate,@color,@year,@service)",conn))
  27.     {
  28.  
  29.         cmd.CommandType = CommandType.Text;
  30.         cmd.Parameters.AddWithValue("@brand", Label1.Text);
  31.         cmd.Parameters.AddWithValue("@model", Label2.Text);
  32.         cmd.Parameters.AddWithValue("@plate", Label3.Text);
  33.         cmd.Parameters.AddWithValue("@color", Label4.Text);
  34.         cmd.Parameters.AddWithValue("@year", Label5.Text);
  35.         cmd.Parameters.AddWithValue("@service", Label6.Text);
  36.  
  37.         conn.Open();
  38.         try
  39.         {
  40.             cmd.ExecuteNonQuery();
  41.         }
  42.         catch(Exception e)
  43.         {
  44.             string errorMessage = e.Message;
  45.             //Set a label on the client to see the error message, or pause in the debugger and examine the property here.
  46.             //throw;
  47.         }
  48.     }
  49. }
  50.        
  51. Button2.Click += Button2_Click;
  52.        
  53. INSERT INTO CarTab(Brand, Model, Plate, Color, Year, Service)