Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 2.33 KB | None | 0 0
  1.  
  2.         protected void Button_Click(object sender, EventArgs e)
  3.         {
  4.            
  5.             LinkButton lb = sender as LinkButton;
  6.  
  7.             SqlConnection con = null;
  8.  
  9.             try
  10.             {
  11.  
  12.                 System.Diagnostics.Debug.WriteLine("Statement trying....");
  13.                 // get the connection to the DB from the web.config connection strings section
  14.                 con = new SqlConnection(ConfigurationManager.ConnectionStrings["WebsiteDBConnectionString"].ConnectionString);
  15.                
  16.                 // Declare your SQL statement to be executed.  
  17.                 // Here, two different SQLs are available depending on the button clicked.
  18.                 string sql = "";
  19.  
  20.                 // determine which button was clicked based on the commandName issued
  21.                 if (lb.CommandName.Equals("Like")) { sql = "INSERT INTO UserVotes(username, excuseID, vote) VALUES (@username, @excuseID, 1)"; }
  22.                 else if (lb.CommandName.Equals("Dislike")) { sql = "INSERT INTO UserVotes(username, excuseID, vote) VALUES (@username, @excuseID, -1)"; }
  23.  
  24.                 int excuseID = Convert.ToInt32(lb.CommandArgument);
  25.  
  26.                 // open the connection to the DB
  27.                 con.Open();
  28.  
  29.                 // Create a command and pass it your various SqlParameters
  30.                 // (here we have one parameter for the PK of the record being updated).
  31.                 SqlCommand cmd = new SqlCommand(sql, con);
  32.                 SqlParameter p = new SqlParameter("excuseID", excuseID);
  33.                 SqlParameter u = new SqlParameter("username", Session["username"]);
  34.                 cmd.Parameters.Add(p);
  35.                 cmd.Parameters.Add(u);
  36.  
  37.                 // Execute the update query
  38.                 cmd.ExecuteNonQuery();
  39.  
  40.                 // close the connection to the DB and re-bind (refresh) the repeater
  41.                 con.Close();
  42.                 GVsortExcuses.DataBind();
  43.  
  44.                 System.Diagnostics.Debug.WriteLine("Statement finished....");
  45.             }
  46.             catch (Exception ex)
  47.             {
  48.                 // print out error messages to the output window (not the web page)
  49.  
  50.                 System.Diagnostics.Debug.WriteLine("Exception catched");
  51.  
  52.                 System.Diagnostics.Debug.WriteLine(ex.Message);
  53.             }
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement