I keep getting this debug error, see my code below, I have gone thru it time and time agian and do not see where the problem is. I have checked and have no NULL values that I'm trying to write back.
~~~~~~~~~~~
Error:
System.NullReferenceException was unhandled by user code
Message="Object variable or With block variable not set."
Source="Microsoft.VisualBasic"
~~~~~~~~~~~~
My Code
Dim DBConnAs SqlConnection
Dim DBAddAsNew SqlCommand
Dim strConnectAsString = ConfigurationManager.ConnectionStrings("ProtoCostConnectionString").ConnectionString
DBConn =New SqlConnection(strConnect)
DBAdd.CommandText ="INSERT INTO D12_MIS (" _
&"CSJ, EST_DATE, RECORD_LOCK_FLAG, EST_CREATE_BY_NAME, EST_REVIEW_BY_NAME, m2_1, m2_2_date, m2_3_date, m2_4_date, m2_5, m3_1a, m3_1b, m3_2a, m3_2b, m3_3a, m3_3b" _
&") values (" _
&"'" & Replace(vbCSJ.Text,"'","''") _
&"', " _
&"'" & Replace(tmp1Date,"'","''") _
&"', " _
&"'" & Replace(tmpRecordLock,"'","''") _
&"', " _
&"'" & Replace(CheckedCreator,"'","''") _
&"', " _
&"'" & Replace(CheckedReviewer,"'","''") _
&"', " _
&"'" & Replace(vb2_1,"'","''") _
&"', " _
&"'" & Replace(tmp2Date,"'","''") _
&"', " _
&"'" & Replace(tmp3Date,"'","''") _
&"', " _
&"'" & Replace(tmp4Date,"'","''") _
&"', " _
&"'" & Replace(vb2_5,"'","''") _
&"', " _
&"'" & Replace(vb3_1a,"'","''") _
&"', " _
&"'" & Replace(vb3_1b,"'","''") _
&"', " _
&"'" & Replace(vb3_2a,"'","''") _
&"', " _
&"'" & Replace(vb3_2b,"'","''") _
&"', " _
&"'" & Replace(vb3_3a,"'","''") _
&"', " _
&"'" & Replace(vb3_3b,"'","''") _
&"')"
DBAdd.Connection = DBConn
DBAdd.Connection.Open()
DBAdd.ExecuteNonQuery()
DBAdd.Connection.Close()
Has nothing to do with NULL values, it's a null reference. In better VB-speak, you've accessed a method or property on an object that you haven't initialized.
For example:
Dim conn as sqlconnection
conn.open
Notice, conn was never set to a sqlconnection, hence, it's nothing (Nothing is vb-speak for C#/C++'s null).
My guess is that you don't actually have your web.config set up correctly. More specifically, you don't have a connection string named "ProtoCostConnectionString" in the connectionstrings section. Note, that is NOT the same as the appsettings section.
Also, you really should use a parameterized query, and if you are going to be concatenating more than a few lines of strings, you'd be much better off using the stringbuilder class if you absolutely MUST continue using string concatenation as how you build your SQL commands.
|||Thanks, I will rewrite this procedure and will use "parameterized query" instead of the string variable.
No comments:
Post a Comment