2014-05-19 58 views
0

我有一個帶有datagridview(ViewCustomersForm)和保存按鈕的窗體。將DataGridView保存爲SQL時,值不能爲NULL dB

我想要做的是讓用戶編輯datagrid視圖中的信息,然後點擊保存按鈕,這將保存值返回到我的SQL表。

不幸的是,當我保存按鈕被按下時,我得到以下錯誤。

「值不能爲空」。

指着這行代碼:

dataadapter.Update(ds.Tables("Customers_table")) 

在下面的代碼的情況下:

Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    'TODO: This line of code loads data into the 'Customers._Customers' table. You can move, or remove it, as needed. 
    Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes" 
    Dim sql As String = "SELECT * FROM Customers" 
    Dim conn As SqlConnection = New SqlConnection(connStr) 
    Dim comm As SqlCommand = New SqlCommand(sql, conn) 
    Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm) 
    Dim ds As DataSet = New DataSet() 

    '---open the connection and fill the dataset--- 
    conn.Open() 
    dataadapter.Fill(ds, "Customers_table") 
    conn.Close() 
    DataGridView1.DataSource = ds 
    DataGridView1.DataMember = "Customers_table" 


End Sub 

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes" 
    Dim sql As String = "SELECT * FROM Customers" 
    Dim conn As SqlConnection = New SqlConnection(connStr) 
    Dim comm As SqlCommand = New SqlCommand(sql, conn) 
    Dim ds As DataSet = New DataSet() 
    Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm) 

    Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter) 
    sqlCmdBuilder.GetUpdateCommand() 
    dataadapter.Update(ds.Tables("Customers_table")) 

End Sub 

我是比較新的VB,所以任何幫助或指針不勝感激。

感謝

+1

數據庫中有一列需要一個值。你沒有爲它提供價值,因此錯誤。 –

+0

我的表中唯一需要的字段是customer_id,我只是試圖更新現有的dB記錄 - 所以這個值已經設置。 – user3580480

回答

0

我需要通過在類級別定義,不與替補變量有。

這裏是工作代碼。

Imports System.Data.SqlClient 
Imports System.Data.Common 

Public Class ViewCustomersForm 
Dim ds As DataSet = New DataSet() 
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes" 
Dim sql As String = "SELECT * FROM Customers" 
Dim conn As SqlConnection = New SqlConnection(connStr) 
Dim comm As SqlCommand = New SqlCommand(Sql, conn) 
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm) 

Private Sub Button1_Click(sender As Object, e As EventArgs) 

End Sub 

Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    '---open the connection and fill the dataset--- 
    conn.Open() 
    dataadapter.Fill(ds, "Customers_table") 
    conn.Close() 
    DataGridView1.DataSource = ds 
    DataGridView1.DataMember = "Customers_table" 


End Sub 

Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click 


    Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter) 
    sqlCmdBuilder.GetUpdateCommand() 
    dataadapter.Update(ds.Tables("Customers_table")) 

End Sub 

End Class