Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys, got an error here. Im trying to add a data to database but the problem is an error show up when I run it. Here is my code
<pre lang="vb">Dim DBConn As New SqlClient.SqlConnection("Data Source=ML0003135586;Integrated Security=SSPI;" & "Initial Catalog=TestSQL")
               Dim DBCmd As New SqlClient.SqlCommand
               Dim DBAdap As New SqlClient.SqlDataAdapter
               Dim DS As New DataSet
               Dim a As Int16
               DBConn.Open()
               Try
                   DBCmd = New SqlClient.SqlCommand("INSERT INTO tblCourses_13514(Course_Code, Course_Title, Objectives, Category, Duration ) VALUES (@Course_Code, @Course_Title, @Objectives, @Category, @Duration)", DBConn)
DBCmd.Parameters.Add("@Course_Code", SqlDbType.NVarChar).Value = drpProCode.Text
DBCmd.Parameters.Add("@Course_Title", SqlDbType.NChar).Value = txtProName.Text
DBCmd.Parameters.Add("@Objectives", SqlDbType.NVarChar).Value = txtProDescription.Text
If drpProCategory.Text = "GEE 51 Bulletin Requirements" Then
                       a = 1
                       DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = a
                   ElseIf drpProCategory.Text = "Cross Functional Courses" Then
                       a = 2
                       DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = a
                   ElseIf drpProCategory.Text = "Functional Courses (online)" Then
                       a = 3
                       DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = a
                   ElseIf drpProCategory.Text = "Fluor University Functional Courses (instructor-led)" Then
                       a = 4
                       DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = a
                   ElseIf drpProCategory.Text = "Local Functional Courses (instructor-led)" Then
                       a = 5
                       DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = a
                   ElseIf drpProCategory.Text = "External Courses" Then
                       a = 6
                       DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = a
                   ElseIf drpProCategory.Text = "Other Courses" Then
                       a = 5
                       DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = a
                   End If
                   DBCmd.Parameters.Add("@Duration", SqlDbType.NVarChar).Value = txtProDuration.Text
                   DBCmd.ExecuteNonQuery()
                   lblProMessage.Text = "Your Proposed Plan was sent for approval"
                   If lblProMessage.Text = "Your Proposed Plan was sent for approval" Then
                       lblProMessage.Text = ""
                       panelAdd.Visible = False
                   End If

But when i run it an an error shows up on the internet explorer
System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Status', table 'TestSQL.dbo.tblCourses_13514'; column does not allow nulls. INSERT fails. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at ASP.home_aspx.btnProSubmit_Click(Object sender, EventArgs e) in C:\Documents and Settings\oca14625\Desktop\TRYING3\HOME.aspx:line 93 


Help me I dont know what the problem is. Thanks in advance and more power
Posted

1 solution

Basically the table tblCourses_13514 has a column named Status and that column is set to not allow NULL values to be inserted. The column also does not have a default value associated with it, so what happens is when you do an INSERT and you do not specify a value for every column, the table will automatically set the value of unspecified columns to their default value(if they have one) or NULL. Since you do not specify the value for Status, and it does not have a default value, it tries to fill the value with NULL, which the column doesn't allow so it throws an error.
 
Share this answer
 
Comments
janwel 4-May-11 22:19pm    
ypu thats the problem alright, thanks .. ive forgot about my table hehehe
JOAT-MON 4-May-11 22:26pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900