Asp.net 无法将数据插入数据库,未显示错误消息

Asp.net 无法将数据插入数据库,未显示错误消息,asp.net,Asp.net,我无法将数据插入数据库,并且没有收到错误消息。通过将patientRIC声明为外键,我链接了表patientRegister和patientMission。下面是代码 patientNOK.cs public class patientNOK { public string nokWardClass { get; set; } public DateTime nokAdDT { get; set; } public string nokFName { get; set;

我无法将数据插入数据库,并且没有收到错误消息。通过将
patientRIC
声明为外键,我链接了表
patientRegister
patientMission
。下面是代码

patientNOK.cs

 public class patientNOK
{
    public string nokWardClass { get; set; }
    public DateTime nokAdDT { get; set; }
    public string nokFName { get; set; }
    public string nokLName { get; set; }
    public string nokNRIC { get; set; }
    public DateTime nokDOB { get; set; }
    public string nokGender { get; set; }
    public string nokNationality { get; set; }
    public string nokRelationship { get; set; }
    public int nokContactH { get; set; }
    public int nokContactHP { get; set; }
    public string nokEmail { get; set; }
    public string nokAddr1 { get; set; }
    public string nokAddr2 { get; set; }
    public string nokState { get; set; }
    public string nokZIP { get; set; }
    public string nokCountry { get; set; }
    public DateTime dischargeDT { get; set; }
    public string patientNRICForiegn { get; set; }
}

 public class patientNOKDAO
{
    string DBConnect = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

    public int InsertNOK(string wardClass , DateTime admissionDT, string nokFName, string nokLName, string nokNRIC, DateTime nokDOB, string nokGender, string nokNationality, string nokRelationship, int nokContactH, int nokContactHP, string nokEmail, string nokAddr1, string nokAddr2, string nokState, int nokZIP, string nokCountry, DateTime dischargeDT, string patientNRIC)
    {
        StringBuilder sqlStr = new StringBuilder();
        int result = 0;
        SqlCommand sqlCmd = new SqlCommand();

        sqlStr.AppendLine("Insert INTO patientAdmission( wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB");

        sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail");

        sqlStr.AppendLine("patientNokAddr1, patientNokAddr2, patientNokZIP, patientNokCountry, dischargeDateTime,patientNRIC)");

        sqlStr.AppendLine("VALUES (@parawardClass,@paraadmissonDT,@parapatientNokFname,@parapatientNokLname,@parapatientNokNRIC, @parapatientNOKDOB,@parapatientNokGender,@parapatientNokNationality");
        sqlStr.AppendLine("@parapatientNokRelationship,@parapatientNokContactH,@parapatientNokContactHP,@parapatientNokEmail,@parapatientNokAddr1,@parapatientNokAddr2,@parapatientNokState,@parapatientNokZIP");
        sqlStr.AppendLine("@parapatientNokCountry,@paradischargeDateTime,@parapatientNRIC)");

        try
        {
            SqlConnection myConn = new SqlConnection(DBConnect);
            sqlCmd = new SqlCommand(sqlStr.ToString(), myConn);

            sqlCmd.Parameters.AddWithValue("@parawardClass", wardClass);
            sqlCmd.Parameters.AddWithValue("@paraadmissionDT", admissionDT);
            sqlCmd.Parameters.AddWithValue("@parapatientNokFname", nokFName);
            sqlCmd.Parameters.AddWithValue("@parapatientNokLname", nokLName);
            sqlCmd.Parameters.AddWithValue("@parapatientNokNRIC", nokNRIC);
            sqlCmd.Parameters.AddWithValue("@parapatientNOKDOB", nokDOB);
            sqlCmd.Parameters.AddWithValue("@parapatientNokGender", nokGender);
            sqlCmd.Parameters.AddWithValue("@parapatientNokNationality", nokNationality);
            sqlCmd.Parameters.AddWithValue("@parapatientNokRelationship", nokRelationship);
            sqlCmd.Parameters.AddWithValue("@parapatientNokContactH", nokContactH);
            sqlCmd.Parameters.AddWithValue("@parapatientNokContactHP", nokContactHP);
            sqlCmd.Parameters.AddWithValue("@parapatientNokEmail", nokEmail);
            sqlCmd.Parameters.AddWithValue("@parapatientNokAddr1", nokAddr1);
            sqlCmd.Parameters.AddWithValue("@parapatientNokAddr2", nokAddr2);
            sqlCmd.Parameters.AddWithValue("@parapatientNokState", nokState);
            sqlCmd.Parameters.AddWithValue("@parapatientNokZIP", nokZIP);
            sqlCmd.Parameters.AddWithValue("@parapatientNokCountry", nokCountry);
            sqlCmd.Parameters.AddWithValue("@paradischargeDateTime", dischargeDT);
            sqlCmd.Parameters.AddWithValue("@parapatientNRIC", patientNRIC);

            myConn.Open();
            result = sqlCmd.ExecuteNonQuery();

            myConn.Close();
        }
        catch(Exception ex)
        {
            logManager log = new logManager();
            log.addLog("patientNOKDAO.InsertNOK", sqlStr.ToString(), ex);
        }
        return result;
    }
}
UI

 string wardPreference = ddl1.SelectedValue;

        string admissionDischargeDT = adDT.Text;
        string admissionDT = admissionDischargeDT.Substring(0,19);
        string dischargeDT = admissionDischargeDT.Substring(22,19);

        DateTime admissionDTinput = DateTime.ParseExact(admissionDT,"MM/dd/yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture);

        DateTime dischargeDTinput = DateTime.ParseExact(dischargeDT, "MM/dd/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);

        string FNameNOK = TextBoxNOKFname.Text;
        string LNameNOK = TextBoxNOKLname.Text;
        string nricNOK = TextBoxNOKNRIC.Text;
       //
        DateTime dobNOK = DateTime.ParseExact(nokDOB.Text.ToString(), "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
        string selectedNOKGender = null;
        if(Request.Form["radio-unstyled-inline-left"] !=null)
        {
            selectedNOKGender = Request.Form["radio-unstyled-inline-left"].ToString();
        }

        string nationalityNok = TextBoxNOKNationality.Text;
        string relationshipNOK = DropDownListRelationship.SelectedValue;

        int nokContactH = Convert.ToInt32(TextBoxNOKContactH.Text);
        int nokContactHP = Convert.ToInt32(TextBoxNOKContactHP.Text);
        string nokEmail = TextBoxNOKContactEmail.Text;
        string nokAddr1 = TextBoxNOKAddr1.Text;
        string nokAddr2 = TextBoxNOKAddr2.Text;
        string nokState = TextBoxNOKState.Text;
        int nokZIP = Convert.ToInt32(TextBoxNOKZIP.Text);
        string nokCountry = TextBoxNOKCountry.Text;
        string patientNRIC = TextBoxNRIC.Text;

        try
        {
            patientNOKDAO fmNOK = new patientNOKDAO();

            int insNOK = fmNOK.InsertNOK(wardPreference, admissionDTinput, FNameNOK, LNameNOK, nricNOK, dobNOK, selectedNOKGender, nationalityNok, relationshipNOK, nokContactH, nokContactHP, nokEmail, nokAddr1, nokAddr2, nokState, nokZIP, nokCountry, dischargeDTinput, patientNRIC);

            if (insNOK == 1)
            {
                TextBoxAddr1.Text = "Sucess";
            }
            else
            {
                TextBoxAddr1.Text = "FAILURE";
            }
        }
        catch (FormatException)
        {
            TextBoxAddr1.Text = "error";
        }
    }
我试着查看日志文件,它显示了这一点,显然NOK类存在问题

异常类型:System.Data.SqlClient.SqlException异常: “patientNokGender”附近的语法不正确。资料来源: patientNOKDAO.InsertNOK sql命令:插入PatientMission( wardClass,admissiont,patientNokFname,patientNokLname, patientNokNRIC,patientNOKDOB patientNokGender,PatientNokNational, patientNokRelationship,patientNokContactH,patientNokContactHP, PatientNokMail patientNokAddr1,patientNokAddr2,patientNokZIP, patientNokCountry、放电时间、PatientRic)值 (@parawardClass、@paraadmissond、@parapatientNokFname、@parapatientNokLname、@parapatientNokNRIC、, @parapatientNOKDOB、@parapatientnokender、@parapatientnokenantity @parapatientNokRelationship、@parapatientNokContactH、@parapatientNokContactHP、@parapatientNokEmail、@parapatientNokAddr1、@parapatientNokAddr2、@parapatientNokState、@parapatientNokZIP @parapatientNokCountry,@paradischargeDateTime,@parapatientNRIC)


您可以测试最终生成的sql命令吗。
在这类问题中,我在microsoft sql server中的一个新查询中运行了sqlCmd。

在字符串连接的某些点上,似乎缺少逗号,第一个是:

患者无性别

另一个是:

patientNokEmail patientNokAddr1

原因如下:

sqlStr.AppendLine("Insert INTO patientAdmission( wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB");

sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail");

您还需要确保将逗号添加到值字符串连接中。

更新下面的行-

sqlStr.AppendLine("Insert INTO patientAdmission( wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB");
sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail");
sqlStr.AppendLine("patientNokAddr1, patientNokAddr2, patientNokZIP, patientNokCountry, dischargeDateTime,patientNRIC)");
sqlStr.AppendLine("VALUES (@parawardClass,@paraadmissonDT,@parapatientNokFname,@parapatientNokLname,@parapatientNokNRIC, @parapatientNOKDOB,@parapatientNokGender,@parapatientNokNationality");
sqlStr.AppendLine("@parapatientNokRelationship,@parapatientNokContactH,@parapatientNokContactHP,@parapatientNokEmail,@parapatientNokAddr1,@parapatientNokAddr2,@parapatientNokState,@parapatientNokZIP");
sqlStr.AppendLine("@parapatientNokCountry,@paradischargeDateTime,@parapatientNRIC)");


在第一行、第二行、第四行和第五行添加“,”以确保正确的SQL格式。

尝试调试并检查日志。执行代码时,
result
的值是多少?您好,我实际上是如何做到的?亲爱的@GX-X请在该行result=您的sqlCmnd上插入断点,然后运行应用程序。然后复制sqlCmd的值并粘贴到MicrosoftSQLServer中的新sql查询窗口中并运行它。
sqlStr.AppendLine("Insert INTO patientAdmission( wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB, ");
sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail, ");
sqlStr.AppendLine("patientNokAddr1, patientNokAddr2, patientNokZIP, patientNokCountry, dischargeDateTime,patientNRIC)");
sqlStr.AppendLine("VALUES (@parawardClass,@paraadmissonDT,@parapatientNokFname,@parapatientNokLname,@parapatientNokNRIC, @parapatientNOKDOB,@parapatientNokGender,@parapatientNokNationality, ");
sqlStr.AppendLine("@parapatientNokRelationship,@parapatientNokContactH,@parapatientNokContactHP,@parapatientNokEmail,@parapatientNokAddr1,@parapatientNokAddr2,@parapatientNokState,@parapatientNokZIP, ");
sqlStr.AppendLine("@parapatientNokCountry,@paradischargeDateTime,@parapatientNRIC)");