首页 > 解决方案 > 在 Mac 上未显示错误消息,但在 Windows 上完美运行

问题描述

更新:所有功能现在都可以正常工作。它正在执行所有必要的验证,但 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);
}

标签: c#sqlasp.net

解决方案


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

String UpdateQuery;

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

cmd = new MySqlCommand(UpdateQuery, con);

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

所以你需要使用:

 cmd = new MySqlCommand(InsertQuery, con);

推荐阅读