Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SQL multiple statements, INSERT not working
- protected void btnLog_Click(object sender, EventArgs e)
- {
- using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString()))
- {
- string username = null;
- string password = null;
- string ipAddress = null;
- SymCryptography cryptic = new SymCryptography();
- SqlCommand cmdSelect = new SqlCommand();
- SqlCommand cmdLog = new SqlCommand();
- SqlDataReader myReader = null;
- cmdSelect.Connection = conn;
- cmdLog.Connection = conn;
- cmdSelect.CommandText = "SELECT * FROM uporabniki WHERE up_ime=@up_ime AND geslo=@geslo";
- cmdSelect.CommandType = CommandType.Text;
- cmdLog.CommandText = "INSERT INTO log (up_ime, ip) VALUES (@up_ime, @ip)";
- cmdLog.CommandType = CommandType.Text;
- cmdSelect.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
- cmdSelect.Parameters.Add("@geslo", SqlDbType.NVarChar, 20).Value = cryptic.Encrypt(tbPwd.Text);
- cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
- cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;
- conn.Open();
- try
- {
- //cmdLog.ExecuteNonQuery(); I tried it here, but it doesn't work
- myReader = cmdSelect.ExecuteReader();
- if (myReader.Read())
- {
- username = myReader["up_ime"].ToString();
- password = myReader["geslo"].ToString();
- Session["rights"] = myReader["pravice"];
- Session["login"] = "OK";
- pravice = true;
- }
- myReader.Close();
- //cmdLog.ExecuteNonQuery(); I tried it here, but it doesn't work
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- conn.Close();
- }
- //I tried to open connection again, but stil INSERT does not work
- /* using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString()))
- {
- string ipAddress = null;
- SqlCommand cmdLog = new SqlCommand();
- cmdLog.Connection = conn;
- cmdLog.CommandText = "INSERT INTO log (up_ime, ip) VALUES (@up_ime, @ip)";
- cmdLog.CommandType = CommandType.Text;
- cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
- cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;
- conn.Open();
- try
- {
- cmdLog.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- conn.Close();
- }
- if (pravice == true)
- {
- Response.Redirect("Default.aspx");
- }
- else
- {
- Response.Redirect("Login.aspx");
- }*/
- }
- INSERT INTO log (up_ime, ip) VALUES (<some time>, <test ip text>)
- cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
- cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;
- cmdLog.Parameters.Add("@up_ime", tbUsr.Text);
- cmdLog.Parameters.Add("@ip", ipAddress);
- conn.Open();
- //cmdLog.ExecuteNonQuery(); I tried it here, but it doesn't work
Add Comment
Please, Sign In to add comment