Я боролся с моим кодом.Когда я пытаюсь выполнить оператор вставки и хранимую процедуру сразу, значение текстового поля для хранимой процедуры становится пустым при сохранении в базе данных.Но когда я выполняю только хранимую процедуру, значение текстового поля содержит данные и сохраняется в базе данных.
Что я пропустил?Вот мой код:
private void recorduserlog()
{
sqlcon.Open();
cmd = new SqlCommand();
cmd.CommandText = "userlogs";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlcon;
cmd.Parameters.Add(new SqlParameter("@id", Main.userid));
cmd.Parameters.Add(new SqlParameter("@fullname", Main.passname));
cmd.Parameters.Add(new SqlParameter("@activitydetails", txtfullname.Text));
cmd.Parameters.Add(new SqlParameter("@userform", 1));
cmd.Parameters.Add(new SqlParameter("@datelog", DateTime.Now.ToString("M/d/yyyy hh:mm:ss")));
cmd.ExecuteNonQuery();
sqlcon.Close();
}
private void btnsave_Click(object sender, EventArgs e)
{
if (txtusername.Text != "" && txtaccesscode.Text != "" && txtfullname.Text != "" && cmbaccessleve.Text != "") //validating the fields whether the fields or empty or not
{
if (txtaccesscode.Text.ToString().Trim().ToLower() == txtconfirm.Text.ToString().Trim().ToLower()) //validating Password textbox and confirm password textbox is match or unmatch
{
string UserName = txtusername.Text;
string Password = adduser.Encrypt(txtaccesscode.Text.ToString()); // Passing the Password to Encrypt method and the method will return encrypted string and stored in Password variable.
SqlConnection conn = new SqlConnection(Properties.Settings.Default.myconnectionstring);
sqlcon.Open();
SqlCommand cmd2 = new SqlCommand("insert into endusers(usern,passw,fullname,accesslevel,stats)values('" + UserName + "','" + Password + "','" + txtfullname.Text + "','" + ssfapclass + "',1)", conn);
cmd2.ExecuteNonQuery();
sqlcon.Close();
sqlcon.Dispose();
MessageBox.Show("Successfully Created a User Account for '" + txtfullname.Text + "'.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
recorduserlog();
}
else
{
MessageBox.Show("Access Code and Confirmation Code doesn't match!.. Please Check..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); //showing the error message if password and confirm password doesn't match
}
}
else
{
MessageBox.Show("Please fill all the fields!..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); //showing the error message if any fields is empty
}
}
}
Еще одна вещь: два SQL-запроса используют одно и то же значение TEXTBOX в качестве параметров.