Guest User

Untitled

a guest
Aug 11th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. SQL multiple statements, INSERT not working
  2. protected void btnLog_Click(object sender, EventArgs e)
  3. {
  4. using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString()))
  5. {
  6. string username = null;
  7. string password = null;
  8. string ipAddress = null;
  9.  
  10. SymCryptography cryptic = new SymCryptography();
  11. SqlCommand cmdSelect = new SqlCommand();
  12. SqlCommand cmdLog = new SqlCommand();
  13. SqlDataReader myReader = null;
  14.  
  15. cmdSelect.Connection = conn;
  16. cmdLog.Connection = conn;
  17.  
  18. cmdSelect.CommandText = "SELECT * FROM uporabniki WHERE up_ime=@up_ime AND geslo=@geslo";
  19. cmdSelect.CommandType = CommandType.Text;
  20. cmdLog.CommandText = "INSERT INTO log (up_ime, ip) VALUES (@up_ime, @ip)";
  21. cmdLog.CommandType = CommandType.Text;
  22.  
  23. cmdSelect.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
  24. cmdSelect.Parameters.Add("@geslo", SqlDbType.NVarChar, 20).Value = cryptic.Encrypt(tbPwd.Text);
  25. cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
  26. cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;
  27.  
  28. conn.Open();
  29. try
  30. {
  31. //cmdLog.ExecuteNonQuery(); I tried it here, but it doesn't work
  32. myReader = cmdSelect.ExecuteReader();
  33. if (myReader.Read())
  34. {
  35. username = myReader["up_ime"].ToString();
  36. password = myReader["geslo"].ToString();
  37. Session["rights"] = myReader["pravice"];
  38. Session["login"] = "OK";
  39. pravice = true;
  40. }
  41. myReader.Close();
  42. //cmdLog.ExecuteNonQuery(); I tried it here, but it doesn't work
  43. }
  44. catch (Exception ex)
  45. {
  46. Console.WriteLine(ex.ToString());
  47. }
  48. conn.Close();
  49. }
  50. //I tried to open connection again, but stil INSERT does not work
  51. /* using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString()))
  52. {
  53. string ipAddress = null;
  54. SqlCommand cmdLog = new SqlCommand();
  55. cmdLog.Connection = conn;
  56. cmdLog.CommandText = "INSERT INTO log (up_ime, ip) VALUES (@up_ime, @ip)";
  57. cmdLog.CommandType = CommandType.Text;
  58. cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
  59. cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;
  60. conn.Open();
  61. try
  62. {
  63. cmdLog.ExecuteNonQuery();
  64. }
  65. catch (Exception ex)
  66. {
  67. Console.WriteLine(ex.ToString());
  68. }
  69. conn.Close();
  70. }
  71.  
  72. if (pravice == true)
  73. {
  74. Response.Redirect("Default.aspx");
  75. }
  76. else
  77. {
  78. Response.Redirect("Login.aspx");
  79. }*/
  80. }
  81.  
  82. INSERT INTO log (up_ime, ip) VALUES (<some time>, <test ip text>)
  83.  
  84. cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
  85. cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;
  86.  
  87. cmdLog.Parameters.Add("@up_ime", tbUsr.Text);
  88. cmdLog.Parameters.Add("@ip", ipAddress);
  89.  
  90. conn.Open();
  91.  
  92. //cmdLog.ExecuteNonQuery(); I tried it here, but it doesn't work
Add Comment
Please, Sign In to add comment