【问题标题】:Not showing error massage on Mac but works perfectly on windows在 Mac 上不显示错误消息,但在 Windows 上完美运行
【发布时间】: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 @

标签: c# sql asp.net


【解决方案1】:

在“InsertNewEmployeeRecord”方法中,您有以下内容:

String UpdateQuery;

InsertQuery = "Insert Into Employee VALUES ('"+SSN+ "','"+DOB+"','"+SFName+"','"+MName+"','"+LName+"','"+Address+"')";

cmd = new MySqlCommand(UpdateQuery, con);

您正在声明 UpdateQuery,然后将值分配给 InsertQuery,然后执行 UpdateQuery,它为 null。

所以你需要使用:

 cmd = new MySqlCommand(InsertQuery, con);

【讨论】:

  • 谢谢插入命令正在工作。但它没有显示任何警报
猜你喜欢
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
相关资源
最近更新 更多