Advertisement
Guest User

salpit

a guest
Oct 17th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. //add SQL namespace
  9. using System.Data.SqlClient;
  10.  
  11. namespace CRUD_Friday
  12. {
  13. public partial class add : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17.  
  18. }
  19.  
  20. protected void Button1_Click(object sender, EventArgs e)
  21. {
  22. //create connection string
  23. string myConnection = @"Data Source=JDTCAD2-22\SQLEXPRESS;Initial Catalog=cinemaDB;Integrated Security=True";
  24.  
  25. //create new SQL connection
  26. SqlConnection con = new SqlConnection(myConnection);
  27.  
  28. //open connection
  29. con.Open();
  30.  
  31. //get VALUE from the webpage
  32. string getTitle = txtTitle.Text;
  33. decimal getRating = Convert.ToDecimal(txtRating.Text);
  34.  
  35. //create SQL query
  36. string query = "INSERT INTO movie(title, rating) VALUES (@myTitle, @myRating)";
  37.  
  38. //add data
  39. SqlCommand cmd = new SqlCommand(query, con);
  40.  
  41. cmd.Parameters.AddWithValue("@myTitle", getTitle);
  42. cmd.Parameters.AddWithValue("@myRating", getRating);
  43.  
  44. cmd.ExecuteNonQuery();
  45. //close connection
  46. con.Close();
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement