对象不能从 DBNull 转换为其他类型。
\n\n我有一个引发上述错误的以下函数。我正在处理数据库和 C# 代码中的所有空值。\n那么它在哪里收到此错误?
\n\n我可以在 catch 块中看到错误。但我不明白以下 create() 中的哪一行出现错误。
\n\n private void btnsearch_Click(object sender, EventArgs e)\n {\n try\n {\n dt.Rows.Clear();\n SqlConnection CN = new SqlConnection(mysql.CON.ConnectionString);\n SqlDataAdapter sda = new SqlDataAdapter("select EMP_EMPLOYEES.NAME,EMP_EMPLOYEES.JOBNAME,EMP_EMPLOYEES.FIA,EMP_EMPLOYEES.DRAGA,EMP_EMPLOYEES.SALARY ,EMP_HASM_DET.GZA,EMP_EMPLOYEES.NAQLBADAL,EMP_EMPLOYEES.JOBBADALAT ,BLADIAINFO.MA3ESHA ,EMP_EMPLOYEES.DISSENT ,EMP_EMPLOYEES.SANDOK as\'\xd8\xa7\xd9\x84\xd8\xb5\xd9\x86\xd8\xaf\xd9\x88\xd9\x82\' from BLADIAINFO ,EMP_EMPLOYEES left join EMP_HASM_DET on EMP_HASM_DET.EMPID = EMP_EMPLOYEES.ID left join EMP_MOKHALFAT_DET on EMP_MOKHALFAT_DET.EMPID = EMP_EMPLOYEES.ID left join EMP_MOKHALFAT on EMP_MOKHALFAT.ID=EMP_MOKHALFAT_DET.MOKHALFAID left join EMP_HASMIAT on EMP_HASMIAT.ID=EMP_HASM_DET.HASMID and EMP_EMPLOYEES.EMPTYPE = \'" + cmbEMPTYPE.SelectedItem.ToString() + "\' ", CN);\n sda.Fill(dt);\n }\n ////////////\n …Run Code Online (Sandbox Code Playgroud) 我需要检查记录是否保存到数据库中。如果它保存在数据库中打开另一个表单,否则显示一条消息,它不在数据库中。
如果记录不在数据库中,我会收到此错误Object reference not set to an instance of an object.
这是我的代码,请帮助我在此处找到错误:
string cmdStr = "Select NO from COM_LIST_EXPORT where NO = '" + txtNO.Text + "'";
SqlCommand cmd = new SqlCommand(cmdStr, CN);
int count = (int)cmd.ExecuteScalar();
if (count == Convert.ToInt32(txtNO.Text))
{
Archive_Sader dd = new Archive_Sader();
dd.Show();
}
else
{
MessageBox.Show("please save first");
}
Run Code Online (Sandbox Code Playgroud) 我多次检查我的SQL语句,似乎我的SQL语句是错误的.我不知道为什么它不起作用.我的SQL语句是正确的,它导致了这个OleDBException"UPDATE语句中的语法错误.".
这是代码
OleDbConnection CN = new OleDbConnection(mysql.CON.ConnectionString);
CN.Open();
cmd1 = new OleDbCommand("Update Mosque Set Name='" + txtNAME.Text + "', No='" + Convert.ToInt32(txtNO.Text) + "', place='" + txtPlace.Text + "', group='" + txtGroup.Text + "', description='" + txtdec.Text + "' where id='" + txtID.Text + "'", CN);
cmd1.ExecuteNonQuery();
CN.Close();
Run Code Online (Sandbox Code Playgroud)
需要帮助请知道这里的错误是什么