【发布时间】:2020-04-21 18:42:13
【问题描述】:
更新:所有功能现在都可以正常工作。它正在执行所有必要的验证,但 Alertmassage(例如:ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('SSN already exists, record is updated.!')", true);)没有显示在屏幕上。 它在 Windows 上完美运行,但在 Mac 上不显示。
protected void BtnBtnInsert_Click(object sender, System.EventArgs e)
{
MySqlCommand cmd;
string str;
MySqlConnection con = new MySqlConnection(ConString);
int Status = 0;
con.Open();
String UpdateQuery;
String SSN;
SSN = TxtBxSSSN.Text.ToString().Trim();
if (CheckValidSSNBeforeUpdate(SSN) == "1")
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('SSN already exists, record is updated.!')", true);
}
else
{
InsertNewEmployee();
}
con.Close();
}
public void InsertNewEmployee()
{
String SSN, SFName, MName, LName, DOB, Address;
SSN = TxtBxSSSN.Text.ToString().Trim();
SFName = lblSFanme.Text.ToString().Trim();
MName = lblMName.Text.ToString().Trim();
LName = lblLName.Text.ToString().Trim();
DOB = lblDOB.Text.ToString().Trim();
Address = lblAddress.Text.ToString().Trim();
String SSN1 = new String(SSN.Where(x => Char.IsDigit(x)).ToArray());
String Tmpe = "SSN : " + SSN + " , SFName : " + SFName + " , MName : " + MName + " , LName : " + LName + " , DOB : " + DOB + " , Address : " + Address;
if (SSN.Length == 0 || SFName.Length == 0 || MName.Length == 0 || LName.Length == 0 || DOB.Length == 0 || Address.Length == 0)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter values in all the fields. All fields are mandatory')", true);
}
else
{
if (SSN1.Length != 9)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Invalid SSN, It must be of 9 digits')", true);
}
else
{
InsertNewEmployeeRecord();
}
}
}
public void InsertNewEmployeeRecord()
{
String SSN, SFName, MName, LName, DOB, Address;
SSN = TxtBxSSSN.Text.ToString().Trim();
SFName = lblSFanme.Text.ToString().Trim();
MName = lblMName.Text.ToString().Trim();
LName = lblLName.Text.ToString().Trim();
DOB = lblDOB.Text.ToString().Trim();
Address = lblAddress.Text.ToString().Trim();
MySqlCommand cmd;
string str;
MySqlConnection con = new MySqlConnection(ConString);
int Status = 0;
con.Open();
String InsertQuery;
InsertQuery = "Insert Into Employee VALUES ('"+SSN+ "','"+DOB+"','"+SFName+"','"+MName+"','"+LName+"','"+Address+"')";
cmd = new MySqlCommand(InsertQuery, con);
cmd.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record inserted successfully.!')", true);
}
【问题讨论】:
-
执行以下操作:从插入状态中删除
'"+SSN+ "',并运行您的代码。如果它向您发送错误消息,则表示它正在按编码工作。 -
在
InsertNewEmployeeRecord中设置断点。你到了吗? -
@AntonioVenerosoContreras 它不发送和错误消息。
-
最小意外原则 - 当代码实际上插入一行时,您应该不将其称为
UpdateQuery(根本不更新)......这对于稍后查看代码的任何人来说都是一种困惑...... -
另外:SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看 @987654322 @