MSSQL Query Error: "Cannot insert the value NULL into column <X>."
Following error occurs in /// this coding "
Cannot insert the value NULL into column 'boarding', table 'C:\USERS\VBI SERVER\DOCUMENTS\VISUAL STUDIO 2008\WEBSITES\VOLVO\APP_DATA\ASPNETDB.MDF.dbo.boardingpt'; column does not allow nulls. INSERT fails. The statement has been terminated.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO boardingpt (service, travelid, bdpt_name, time) VALUES('开发者_StackOverflow社区" & Trim(TextBox3.Text.ToString) & "', '" & Trim(TextBox4.Text.ToString) & "', '" & Trim(TextBox1.Text.ToString) & "','" & Trim(TextBox2.Text.ToString) & "')"
cmd.ExecuteNonQuery()
con.Close()
End Sub
You need to change the Column to an IDENTITY
or under SSMS in the column defintion select
Table Designer -> Identity Specification -> (Is Identity)
to true
You are not providing a value for the boarding
field, which is a NOT NULL
field (Allow Nulls
is not checked).
Being a NOT NULL
field means you have to provide a value for it.
If the field is supposed to be the ID of the rows in the table, you should make it into an IDENTITY
field.
See this on MSDN for how to change the identity properties using the Visual Studio Database designer.
you did not set the boarding column to be Identity
, so is trying to put a null in there (as you haven't provided its value in the insert query)
You are trying to insert NULL value for the column which is set to NOT NULL.
Solution
- Make boarding column to ALLOW NULL and remove Primary key (because primary key never can be null)
or
- Set IDENTITY to YES in your table design mode for boarding column
Hope this helps
Thanks Vivek
精彩评论