如何将数据从DataGridView更新到数据库?错误 - 无法插入值NULL

时间:2016-02-20 12:19:44

标签: c# sql sql-server datagridview

我正在尝试将数据从DataGridView更新到我的数据库。当我在谷歌上寻找这个问题的解决方案时,我注意到所有的解决方案都是通过使用类变量来管理的(对于DataTableSqlDataAdapter,...)。我试图通过使用函数变量来做到这一点。

这是我将数据加载到DataGridView的方式:

private void LoadDataGridView(int ID)
{
    try
    {
         SqlConnection connection = new SqlConnection(connString);
         SqlCommand cmd = new SqlCommand("SELECT SENTENCE FROM Sentences WHERE CategoryID = @ID",connection);
         cmd.Parameters.Add("@ID",SqlDbType.Int).Value = ID;
         DataTable dataTable = new DataTable();

         SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
         dataAdapter.Fill(dataTable);

         DataGridView.DataSource = dataTable;
    }
    catch (Exception)
    {
         MessageBox.Show("ERROR WHILE CONNECTING TO DATABASE!");
    }
}

DataGridView仅显示与特定ID匹配的句子。

这是我到目前为止所尝试的:

private void RefreshBtn_Click(object sender, EventArgs e)
{
    try
    {
          SqlConnection connection = new SqlConnection(connString);
          SqlCommand cmd = new SqlCommand("SELECT SENTENCE FROM Sentences WHERE CategoryID IN(@CategoryID)", connection);
          cmd.Parameters.Add("@CategoryID",SqlDbType.Int).Value = CategoryID;

          SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
          SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(dataAdapter);
          dataAdapter.Update(dataTable);
    }
    catch (Exception)
    {
          MessageBox.Show("ERROR WHILE CONNECTING WITH DATABASE!");
    }
}

这是出现的错误:

  

无法将值NULL插入列CategoryID,table ...;列不允许nulls.Insert失败。声明已经终止。

2 个答案:

答案 0 :(得分:0)

如果您的Id数据类型为int,则不需要将其放入""。

        private void RefreshBtn_Click(object sender, EventArgs e)
    {
        try
        {
            string connString;//WHAT EVER IT IS
            string Query=string.Format(@"SELECT QUESTION FROM Questions WHERE CategoryID = '{0}'",CategoryID);
              SqlConnection connection = new SqlConnection(connString);
              SqlCommand cmd = new SqlCommand(Query, connection);
    connection.Open();
 cmd.ExecuteNonQuery();
              SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd,connString);

              DataTable DataTable=new DataTable();
             dataAdapter.Fill(DataTable);
             datagridview1.DataSource=DataTable;
        }
        catch (Exception)
        {
              MessageBox.Show("ERROR WHILE CONNECTING WITH DATABASE!");
        }
        catch   (SqlException)
        {
        MessageBox.Show("SqL Error!");

        }
    }

答案 1 :(得分:0)

解决我的问题: 首先,我在DataGridView中显示了数据库表的所有列,然后我隐藏了Primary Key列和CategoryID列。这显示在以下代码中:

private void LoadDataGridView(int ID)
{
    try
    {
         SqlConnection connection = new SqlConnection(connString);
         SqlCommand cmd = new SqlCommand();
         cmd.CommandText = "SELECT * FROM Sentences WHERE CategoryID = @ID";
         cmd.Connection = connection;
         cmd.Parameters.Add("@ID",SqlDbType.Int).Value = ID;

         dataTable = new DataTable();

         SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
         dataAdapter.Fill(dataTable);

         dataGridView.DataSource = dataTable;
         dataGridView.Columns[0].Visible = false;
         dataGridView.Columns[2].Visible = false;
   }
   catch (Exception)
   {
         MessageBox.Show("ERROR WHILE CONNECTING WITH DATABASE!");
   }
}

然后,我仅为CategoryID列设置了默认值,因为Primary Key列的选项为Auto - Increment。这在以下代码中显示:

private void dataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
    e.Row.Cells["CategoryID"].Value = CategoryID;
}

我做的最后一件事是实现RefreshBtn的代码(以下代码实际上与相关代码相同):

private void RefreshBtn_Click(object sender, EventArgs e)
{
    try
    {
         SqlConnection connection = new SqlConnection(connString);
         SqlCommand cmd = new SqlCommand();
         cmd.CommandText = "SELECT * FROM Questions WHERE CategoryID = @CategoryID";
         cmd.Connection = connection;
         cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = CategoryID;

         SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
         SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(dataAdapter);
         dataAdapter.Update(dataTable);
    }
    catch (Exception)
    {
         MessageBox.Show("ERROR WHILE CONNECTING WITH DATABASE !");
    }
}