I can't seem to figure out why the below code is giving me the following error:
Object reference not set to an instance of an object.
Line 47: objSQLCommand.Connection.Open()
This is the problem code:
Function getTabContent() As String
Dim strUserInitials() As String = Request.ServerVariables("LOGON_USER").Split(CChar("\"))
strUser = LCase(Trim(strUserInitials(strUserInitials.GetUpperBound(0)))).ToString()
objStringBuilder = New StringBuilder()
For intColumn As Integer = 1 To 3
objSQLCommand = New SqlCommand("select c.*, w.* " & _
"from intranet.dbo.tabs t " & _
"inner join intranet.dbo.columns c on t.id = c.tabs_id " & _
"inner join intranet.dbo.widgets w on c.widgets_id = w.widget_id " & _
"where t.is_default = 0 " & _
"and t.cms_initials = #user " & _
"and t.id = #tab " & _
"and c.sort_column = #column " & _
"and w.inactive = 0 " & _
"order by c.tabs_id, c.sort_column, c.sort_row", objSQLConnection)
objSQLCommand.Parameters.Add("#user", SqlDbType.VarChar, 3).Value = strUser
objSQLCommand.Parameters.Add("#tab", SqlDbType.Int, 4).Value = Request.QueryString("tab")
objSQLCommand.Parameters.Add("#column", SqlDbType.Int, 4).Value = intColumn
objStringBuilder.Append("<div class=""column"" id=""column_" & intColumn & """>")
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
While objSQLDataReader.Read()
objStringBuilder.Append("<div class=""portlet"" id=""portlet_" & objSQLDataReader("widget_id") & """>")
objStringBuilder.Append("<div class=""portlet-header"">" & objSQLDataReader("widget_name") & "</div>")
objStringBuilder.Append("<div class=""portlet-content"">" & objSQLDataReader("widget_description") & "</div>")
objStringBuilder.Append("</div>")
End While
objSQLDataReader.Close()
objSQLCommand.Connection.Close()
objStringBuilder.Append("</div>")
Next intColumn
Return objStringBuilder.ToString
End Function
The problem appears to be that the Connection property on objSqlCommand is Nothing and hence you get a NullReferenceException when you access it. This means in all likely hood that objSqlConnection is also Nothing and that is likely the root cause of your problem.
Connection is an object of type SqlConnection. You need to create an instance of this object before you can interact with it. Something like this:
Dim connection As New SqlConnection("your_connection_string")
objSQLCommand.Connection = connection
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
Instead of doing this
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
While objSQLDataReader.Read()
objStringBuilder.Append("<div class=""portlet"" id=""portlet_" & objSQLDataReader("widget_id") & """>")
objStringBuilder.Append("<div class=""portlet-header"">" & objSQLDataReader("widget_name") & "</div>")
objStringBuilder.Append("<div class=""portlet-content"">" & objSQLDataReader("widget_description") & "</div>")
objStringBuilder.Append("</div>")
End While
objSQLDataReader.Close()
objSQLCommand.Connection.Close()
just do
objSQLConnection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
While objSQLDataReader.Read()
objStringBuilder.Append("<div class=""portlet"" id=""portlet_" & objSQLDataReader("widget_id") & """>")
objStringBuilder.Append("<div class=""portlet-header"">" & objSQLDataReader("widget_name") & "</div>")
objStringBuilder.Append("<div class=""portlet-content"">" & objSQLDataReader("widget_description") & "</div>")
objStringBuilder.Append("</div>")
End While
objSQLConnection.Close()
Related
So my code is going through the various components and creates bundles using the BuildKnittingRecords sub for the various components. My code works well when it creates the bundles for the Case "Sleeve", "Front", "Body", "Back" but when it gets to Case "Collar22-32" it gets stuck in the do until loop and as a result will keep on adding to PnlsToProduce(until it crashes cos the number is too big) instead of only running through the do until loop when it finds that component
Private Sub knittingdetailsheaderdataset()
bundlenum = 1
Dim SQL As String
Dim Adapter As New OleDbDataAdapter
Dim dt As New DataTable
Using con As New OleDbConnection(cnString)
Dim cmd As New OleDbCommand()
SQL = "Select bla bla
FROM bla bla
INNER Join bla bla"
(it's a very long sql statement, and not relevent to my issue)
cmd.Connection = con
cmd.CommandText = SQL
Adapter.SelectCommand = cmd
Adapter.Fill(dt)
For dr As Integer = 0 To dt.Rows.Count - 1
componentName = dt(dr)(6)
knittOrderID = dt(dr)(4)
componentID = dt(dr)(3)
sizeID = dt(dr)(5)
qtyppnl = dt(dr)(10)
Select Case componentName
Case "Sleeve", "Front", "Body", "Back"
MxBndleSz = 36
PnlsToProduce = dt(dr)(11)
BuildKnittingRecords(MxBndleSz, componentID, sizeID, PnlsToProduce, qtyppnl, knittOrderID, bundlenum)
Case "Collar22-32"
MxBndleSz = 200
PnlsToProduce = 0
Do Until componentName <> "Collar22-32" Or dr = dt.Rows.Count - 1
PnlsToProduce += dt(dr)(11)
Loop
Select Case PnlsToProduce
Case 50 To 100
PnlsToProduce = PnlsToProduce + 1
Case 101 To 250
PnlsToProduce = PnlsToProduce + 2
Case Is > 250
PnlsToProduce = PnlsToProduce + 3
End Select
BuildKnittingRecords(MxBndleSz, componentID, sizeID, PnlsToProduce, qtyppnl, knittOrderID, bundlenum)
End Select
Next
End Using
End Sub
Hence my question is, why is it doing this and how do I solve it?
This is my code for the BuildknittingRecord() sub, not sure if it's relevant in solving my issue:
Private Sub BuildKnittingRecords(ByRef MaxBundleSz As Integer, ByRef compID As Integer, ByRef sizeID As Integer, ByRef PnlsToproduce As Integer, ByRef QtyPerPanel As Integer, ByRef KnittingOrderID As Integer, ByRef bundlenum As Integer)
pnls2prod = PnlsToproduce
Dim cmdstring As String
Do Until pnls2prod < 10
If bundlenum < 10 Then
bundleNo = txtbatchno.Text & "-K0" & bundlenum
Else
bundleNo = txtbatchno.Text & "-K" & bundlenum
End If
bundlenum += 1
If pnls2prod <= MaxBundleSz Then
PanelsToMake = pnls2prod
cmdstring = " INSERT INTO [KN - KnittingDetailsHeader] (BatchNo, BundleNo, ComponentID, SizeID, PanelsToMake, QtyPerPanel, KnittingOrderID) VALUES('" & txtbatchno.Text & "', '" & bundleNo & "', " & compID & ", " & sizeID & ", " & PanelsToMake & ", " & QtyPerPanel & ", " & KnittingOrderID & ");"
Using con As New OleDbConnection(cnString)
Dim cmd As New OleDbCommand(cmdstring)
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Connection.Open()
cmd.ExecuteNonQuery()
End Using
Exit Do
Else
If pnls2prod < 10 Then
PanelsToMake = pnls2prod + MaxBundleSz
cmdstring = " INSERT INTO [KN - KnittingDetailsHeader] (BatchNo, BundleNo, ComponentID, SizeID, PanelsToMake, QtyPerPanel, KnittingOrderID) VALUES('" & txtbatchno.Text & "', '" & bundleNo & "', " & compID & ", " & sizeID & ", " & PanelsToMake & ", " & QtyPerPanel & ", " & KnittingOrderID & ");"
Using con As New OleDbConnection(cnString)
Dim cmd As New OleDbCommand(cmdstring)
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Connection.Open()
cmd.ExecuteNonQuery()
End Using
Exit Do
End If
PanelsToMake = MaxBundleSz
pnls2prod = pnls2prod - MaxBundleSz
cmdstring = " INSERT INTO [KN - KnittingDetailsHeader] (BatchNo, BundleNo, ComponentID, SizeID, PanelsToMake, QtyPerPanel, KnittingOrderID) VALUES('" & txtbatchno.Text & "', '" & bundleNo & "', " & compID & ", " & sizeID & ", " & PanelsToMake & ", " & QtyPerPanel & ", " & KnittingOrderID & ");"
Using con As New OleDbConnection(cnString)
Dim cmd As New OleDbCommand(cmdstring)
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Connection.Open()
cmd.ExecuteNonQuery()
End Using
End If
Loop
End Sub
Am using Telerik RadGantt chart. i need to insert the event to the data base for that am using the code as given:
Private Sub RadGantt1_TaskUpdate(ByVal sender As Object, ByVal e As Telerik.Web.UI.Gantt.TaskEventArgs) Handles RadGantt1.TaskUpdate
mssql = "insert into project " & _
" (ParentID, OrderID, Title, Start, End, PercentComplete, Expanded, Summary)" & _
" values('E1','" & e.Tasks.GetEnumerator.Current.ID & "', " & _
" '" & e.Tasks.GetEnumerator.Current.Title & "'," & _
" '" & e.Tasks.GetEnumerator.Current.Start & "'," & _
" '" & e.Tasks.GetEnumerator.Current.End & "'," & _
" '" & e.Tasks.GetEnumerator.Current.PercentComplete & "'," & _
" '" & e.Tasks.GetEnumerator.Current.Expanded & "'," & _
" '" & e.Tasks.GetEnumerator.Current.Summary & "')"
Dim mycommand As OdbcCommand
mycommand = New OdbcCommand(mssql, dbcon)
dbcon.Open()
Dim mnresult As Integer = mycommand.ExecuteNonQuery()
If mnresult = 1 Then
EventAdd = False
End If
End Sub
But it gives an object reference error in e.Tasks.GetEnumerator.Current.ID. then how can i take the value from this event?
e.Tasks is a collection so you should iterate through it (e.g., in a for..each loop).
To get a single tasks whose properties you can read you should use the enumerator of the collection, for example e.Tasks(1).GetEnumerator.Current.ID
I'm using a SqlDataReader to retrieve database information to put into a javascript string. I get SqlDataReader.HasRows=True, and if I call my string building without reading the data reader first, I get the proper string. If I try to loop on the reader though, nothing comes out.
The following code returns a properly formed string, but only for the first record:
Function sFnLoadCodingLines() As String
Dim cmdCoding As SqlCommand, drCoding As SqlDataReader, con As New SqlConnection(strcon)
Dim sTemp As String = ""
Dim lInvoiceID As Long
If Not IsNumeric(CLng(Page.Request.QueryString("InvoiceID"))) Then
sFnLoadCodingLines = ""
Exit Function
End If
lInvoiceID = CLng(Page.Request.QueryString("InvoiceID"))
cmdCoding = New SqlCommand
cmdCoding.CommandText = "SELECT BU,DEPT,AccNo,Qty,Amount,Description,EMP,VARIETAL,BRAND,STATE,PROJECT " & _
"FROM OI_tblCoding WHERE isDeleted=0 AND InvoiceID=" & lInvoiceID
cmdCoding.Connection = con
con.Open()
drCoding = cmdCoding.ExecuteReader
drCoding.Read()
If drCoding.HasRows() Then
sTemp = sTemp & "addRowToTable(""" & drCoding("BU").ToString & _
""",""" & drCoding("DEPT").ToString & _
""",""" & drCoding("AccNo").ToString & _
""",""" & drCoding("Qty").ToString & _
""",""" & drCoding("Amount").ToString & _
""",""" & drCoding("Description").ToString & _
""",""" & drCoding("EMP").ToString & _
""",""" & drCoding("VARIETAL").ToString & _
""",""" & drCoding("BRAND").ToString & _
""",""" & drCoding("STATE").ToString & _
""",""" & drCoding("PROJECT").ToString & """);" & vbCrLf
sFnLoadCodingLines = sTemp
Else
sFnLoadCodingLines = ""
End If
con.Close()
End Function
The following record returns an empty string:
Function sFnLoadCodingLines() As String
Dim cmdCoding As SqlCommand, drCoding As SqlDataReader, con As New SqlConnection(strcon)
Dim sTemp As String = ""
Dim lInvoiceID As Long
If Not IsNumeric(CLng(Page.Request.QueryString("InvoiceID"))) Then
sFnLoadCodingLines = ""
Exit Function
End If
lInvoiceID = CLng(Page.Request.QueryString("InvoiceID"))
cmdCoding = New SqlCommand
cmdCoding.CommandText = "SELECT BU,DEPT,AccNo,Qty,Amount,Description,EMP,VARIETAL,BRAND,STATE,PROJECT " & _
"FROM OI_tblCoding WHERE isDeleted=0 AND InvoiceID=" & lInvoiceID
cmdCoding.Connection = con
con.Open()
drCoding = cmdCoding.ExecuteReader
drCoding.Read()
If drCoding.HasRows() Then
While drCoding.Read()
'addRowToTable(sBU, sDPT, sAcct, dbQty, dbAmt, sDesc, sEmp, sVar, sBrand, sState, sProj)
sTemp = sTemp & "addRowToTable(""" & drCoding("BU").ToString & _
""",""" & drCoding("DEPT").ToString & _
""",""" & drCoding("AccNo").ToString & _
""",""" & drCoding("Qty").ToString & _
""",""" & drCoding("Amount").ToString & _
""",""" & drCoding("Description").ToString & _
""",""" & drCoding("EMP").ToString & _
""",""" & drCoding("VARIETAL").ToString & _
""",""" & drCoding("BRAND").ToString & _
""",""" & drCoding("STATE").ToString & _
""",""" & drCoding("PROJECT").ToString & """);" & vbCrLf
End While
sFnLoadCodingLines = sTemp
Else
sFnLoadCodingLines = ""
End If
con.Close()
End Function
But you ARE reading the data in both cases. You call ExecuteReader and then you call Read immediately after that, before testing HasRows. Get rid of that first Read call and your loop will work as you expect. The first code would then fail as it should because you will not be calling Read at all.
Set RsItem = Conn.Execute("EXEC E_UpdateDevBehaviourSmalls #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID")& " ,#cCompID=" & cCompetenceid & " ,#reason=" &reason & " ,#comptype=" & comptype &",#GID=" & GID & " ,#Behaviour='" & MakeSendable(Behaviour) & "' ,#Deadline='" & deadlinedatetime & "' ,#DevBehaviour='" & MakeSendable(DevBehaviour) & "' ,#Why='" & MakeSendable(Why) & "' ,#ExtraNote='" & MakeSendable(ExtraNote) & "'")
if GID = 0 then
if not RsItem.eof then
GID = RsItem.fields(0).value
if reason = 0 then
'add dummy devbehaviour detail
Set RsItem =Conn.Execute("EXEC E_UpdateDevBehaviourDetail #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & GID & " ,#DID=0 ,#TextField1='dummy' ,#educ= 0 ,#TextField2='dummy' ,#TextField3='dummy' ,#TextField4='dummy'")
end if
end if
end if
When I try to execute the code above (full code below) I got the following error:
(It gets stuck at the following part: if not RsItem.fields(0).value)
ADODB.Recordset error '800a0e78'
Operation is not allowed when the object is closed.
Can anyone help me with this error?
<%
Dim DID
Dim GID
Dim cCompetenceid
Dim Behaviour
Dim Deadline
Dim DevBehaviour
Dim Why
Dim ExtraNote
MakeConn
Session("OnlinePageID") = 106
InsertLogItem "S:12"
If Session("EUserType")=1 Then
If UCase(Request("Action"))="SAVEDETAIL" Then
If not Request("DID")="" Then
DID = Request("DID")
Else
DID = 0
End If
Conn.Execute("EXEC E_UpdateDevBehaviourDetail #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID") & " ,#DID=" & DID & " ,#TextField1='" & MakeSendable(Request("TextField1")) & "' ,#TextField2='" & MakeSendable(Request("TextField2")) & "' ,#TextField3='" & MakeSendable(Request("TextField3")) & "' ,#TextField4='" & MakeSendable(Request("TextField4")) & "'")
Conn.Execute("EXEC E_SignIDP #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#SignStatus=0")
End If
End If
'response.write Session("EUserType") & "<br>"
If Session("EUserType")=1 Then
Select Case UCase(Request("Action"))
Case "EDIT"
If not Request("GID")="" Then
'response.write "EXEC E_GetDevBehavior #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID") & "<br>"
Set RsItem = Conn.Execute("EXEC E_GetDevBehavior #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID"))
If not RsItem.EOF Then
Behaviour = Replace (RsItem("Behaviour"),"''","'")
Deadline = RsItem("Deadline")
DevBehaviour = Replace (RsItem("DevBehaviour"),"''","'")
Why = Replace (RsItem("Why"),"''","'")
ExtraNote = Replace (RsItem("ExtraNote"),"''","'")
Else
Response.End
End If
'RsItem.close
'Set RsItem = nothing
Else
End If
Case "SAVE"
If not Request("GID")="" Then
Set RsItem = Conn.Execute("EXEC E_GetDevBehavior #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & Request("GID"))
If not RsItem.EOF Then
Behaviour = Replace (RsItem("Behaviour"),"''","'")
Deadline = RsItem("Deadline")
DevBehaviour = Replace (RsItem("DevBehaviour"),"''","'")
Why = Replace (RsItem("Why"),"''","'")
ExtraNote = Replace (RsItem("ExtraNote"),"''","'")
Else
Response.End
End If
'RsItem.close
'Set RsItem = nothing
end if
dag = Day(Now())
maand = Month(Now())
jaar = Year(Now())
uur = Hour(Time)
minuten = Minute(Time)
seconden = Second(Time)
if len(dag)< 2 then dag ="0" & dag
if len(maand) < 2 then maand ="0" & maand
if len(uur) < 2 then uur ="0" & uur
if len(minuten) < 2 then minuten ="0" & minuten
if len(seconden) < 2 then seconden ="0" & seconden
datum= jaar & "-" & maand & "-" & dag
tijd = uur & ":" & minuten& ":" & seconden
datumtijd = datum & " " & tijd
Conn.Execute("EXEC E_UpdatePOPStartDate #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID"))
Conn.Execute("EXEC E_UpdateStartDate #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#StartDate='" & datumtijd & "'")
If not Request("GID")="" Then
GID = Request("GID")
newcomp = false
Else
GID = 0
Deadline = CDate(FormatDate("31/12/"&year(now)))
newcomp = true
End If
'response.write Request("cCompetenceid") &"<br>"
if not Request("cCompetenceid") = "" then
cCompetenceid = Request("cCompetenceid")
else
cCompetenceid = 0
end if
if not Request("reason") = "" then
reason = Request("reason")
else
reason = 0
end if
if not Request("comptype") = "" then
comptype = Request("comptype")
else
comptype = 1
end if
Select Case (Request("COMPID"))
Case 1460
Behaviour = Request("Behaviour")
Case 1461
Deadline = Request("DeadlineDay") & "-" & Request("DeadlineMonth") & "-" & Request("DeadlineYear")
Deadline = CDate(FormatDate(Deadline))
Case 1462
DevBehaviour = Request("DevBehaviour")
Case 1463
Why = Request("Why")
Case 1464
ExtraNote = Request("ExtraNote")
End Select
deadlinedate = CDate(Deadline)
deadlineyear = year(deadlinedate)
deadlinemonth = month(deadlinedate)
deadlineday = day(deadlinedate)
deadlinedatetime = deadlineyear & "-" & deadlinemonth & "-" & deadlineday & " 00:00:00"
'response.write "EXEC E_UpdateDevBehaviourSmalls #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID")& " ,#cCompID=" & cCompetenceid & " ,#reason=" &reason & " ,#comptype=" & comptype &",#GID=" & GID & " ,#Behaviour='" & MakeSendable(Behaviour) & "' ,#Deadline='" & deadlinedatetime & "' ,#DevBehaviour='" & MakeSendable(DevBehaviour) & "' ,#Why='" & MakeSendable(Why) & "' ,#ExtraNote='" & MakeSendable(ExtraNote) & "'"
'response.end
Set RsItem = Conn.Execute("EXEC E_UpdateDevBehaviourSmalls #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID")& " ,#cCompID=" & cCompetenceid & " ,#reason=" &reason & " ,#comptype=" & comptype &",#GID=" & GID & " ,#Behaviour='" & MakeSendable(Behaviour) & "' ,#Deadline='" & deadlinedatetime & "' ,#DevBehaviour='" & MakeSendable(DevBehaviour) & "' ,#Why='" & MakeSendable(Why) & "' ,#ExtraNote='" & MakeSendable(ExtraNote) & "'")
if GID = 0 then
if not RsItem.eof then
GID = RsItem.fields(0).value
if reason = 0 then
' add dummy devbehaviour detail
Set RsItem =Conn.Execute("EXEC E_UpdateDevBehaviourDetail #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#GID=" & GID & " ,#DID=0 ,#TextField1='dummy' ,#educ= 0 ,#TextField2='dummy' ,#TextField3='dummy' ,#TextField4='dummy'")
end if
end if
end if
'RsItem.close
'Set RsItem = nothing
Conn.Execute("EXEC E_SignIDP #ClientID=" & Session("ClientID") & " ,#UserID=" & Session("EUserID") & " ,#SignStatus=0")
if newcomp = true then
tempstr = "../popoverview.asp"
ClientScript("parent.location.href = '../bottomframe.asp?GID=" & GID & "&" & SetID &"&ViewID=4'" )
else
response.write " in"
tempstr = "compoverview.asp?Action=Edit&GID="&GID
response.redirect tempstr
' ClientScript("location.href =" & tempstr)
end if
End Select
End If
conn.close
set conn= nothing
%>
Stored procedure:
USE [Q]
GO
/****** Object: StoredProcedure [dbo].[E_UpdateDevBehaviourSmalls] Script Date: 17/10/2013 15:05:53 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[E_UpdateDevBehaviourSmalls]
(#ClientID int,
#UserID int,
#cCompID int,
#reason int,
#comptype int,
#GID int,
#Behaviour varchar(250),
#Deadline datetime,
#DevBehaviour text,
#Why text,
#ExtraNote text)
AS
If (#GID = 0)
BEGIN
INSERT INTO DevBehaviour(ClientID,UserID,Behaviour,Deadline,DevBehaviour,Why,ExtraNote,cCompId,reason,comptype)
VALUES(#ClientID,#UserID,#Behaviour,#Deadline,#DevBehaviour,#Why,#ExtraNote, #cCompID,#reason,#comptype)
SELECT ##identity
END
Else
BEGIN
UPDATE DevBehaviour
SET Behaviour=#Behaviour, Deadline=#Deadline, DevBehaviour=#DevBehaviour, Why=#Why, ExtraNote=#ExtraNote, cCompId = #cCompID, reason = #reason, comptype = #comptype
WHERE (ClientID = #ClientID) AND (UserID = #UserID) AND (GID = #GID)
END
I was able to reproduce your problem. Please try the stored proc below (using nocount)
USE [Q]
GO
/****** Object: StoredProcedure [dbo].[E_UpdateDevBehaviourSmalls] Script Date: 17/10/2013 15:05:53 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[E_UpdateDevBehaviourSmalls]
(#ClientID int,
#UserID int,
#cCompID int,
#reason int,
#comptype int,
#GID int,
#Behaviour varchar(250),
#Deadline datetime,
#DevBehaviour text,
#Why text,
#ExtraNote text)
AS
set nocount on
If (#GID = 0)
BEGIN
INSERT INTO DevBehaviour(ClientID,UserID,Behaviour,Deadline,DevBehaviour,Why,ExtraNote,cCompId,reason,comptype)
VALUES(#ClientID,#UserID,#Behaviour,#Deadline,#DevBehaviour,#Why,#ExtraNote, #cCompID,#reason,#comptype)
SELECT ##identity
END
Else
BEGIN
UPDATE DevBehaviour
SET Behaviour=#Behaviour, Deadline=#Deadline, DevBehaviour=#DevBehaviour, Why=#Why, ExtraNote=#ExtraNote, cCompId = #cCompID, reason = #reason, comptype = #comptype
WHERE (ClientID = #ClientID) AND (UserID = #UserID) AND (GID = #GID)
END
set nocount off
I have this code and its coming up with an INSERT INTO statement error...
Its probably something but I have been at it for a while... please help.
'Add items to db'
Function recordOrder()
objDT = Session("Cart")
Dim intCounter As Integer
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath2 & ";"
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
myCommand2.CommandText = "INSERT INTO order(order_date, coupon_id, customer_id, quantity) values('" & System.DateTime.Now & "','" & Int32.Parse(objDR("ID")) & "','" & Int32.Parse(custID) & "','" & Int32.Parse(objDR("quantity")) &"')"
myCommand2.Connection = con2
con2.Open()
myCommand2.ExecuteReader()
con2.Close()
test.Text += "Order ID: " & objDR("ID") & "Order Date: " & System.DateTime.Now & ", Cust ID: " & custID & ", Quantity: " & objDR("quantity") &" "
Next
End Function
I think you are getting an error by not enclosing the Date inside Pound signs. You have to do this in Jet (Access) when using variables not parameters.
VALUES('#" & DateTime.Now.Date & "#',...
I also took the liberty of refactoring this code for you since you are creating a new connection for each record which is bad news. Use a Try Catch Finally block and move all that stuff outside the For Loop (please see below)
Function recordOrder()
objDT = Session("Cart")
Dim intCounter As Integer
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" <-- etc
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
myCommand2.Connection = con2
con2.Open()
Try
For intCounter = 0 To obDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
myCommand2.CommandText = "INSERT INTO order(order_date,coupon_id,customer_id,quantity)" _
& "VALUES ('#" & System.DateTime.Now.Date & "#','" & Int32.Parse(objDR("ID")) & "','" & Int32.Parse(custID) _
& "','" & Int32.Parse(objDR("quantity")) & "')"
myCommand2.ExecuteReader()
Next
Catch ex As Exception
'handle errors here
Finally
If con2.State = ConnectionState.Open Then
con2.Close()
End If
End Try
End Function
Remember to mark as answered if this helps.
I've sorted it out by removing the single quotes. Thanks everybody to contributed to this.