J
jeff
when i use parameters to update a record (in access 2000),
and i replace a text field's value with an empty string
(by erasing the asp:textbox content and firing the
DataGrid's update command), my TRY block catches the
exception (Parameter @Abstract has no default value).
when i rewrote my code to refrain from using parameters i
received no error, and the update works flawlessly.
standard procedure would be to use parameters. how do i
get mine to work?
rem *****************************************************
MY ORIGINAL CODE (catches exception):
'Initialize variables:
Dim tb As TextBox
Dim sql As String = "UPDATE tblEvents " & _
"SET eveDate=@Date, eveTitle=@Title, " & _
"eveAbstract=@Abstract WHERE eveID=@intID;"
Dim cmd As New OleDBCommand( sql, con )
'Assign values to query parameters:
tb = e.Item.FindControl("txtDate")
cmd.Parameters.Add( "@Date", CDate(tb.Text) )
tb = e.Item.FindControl("txtTitle")
cmd.Parameters.Add( "@Title", Replace( tb.Text, _
"'", "''" ) )
tb = e.Item.FindControl("txtAbstract")
cmd.Parameters.Add( "@Abstract", Replace( tb.Text, _
"'", "''" ) )
cmd.Parameters.Add( "@intID", EventGrid.DataKeys( _
CInt(e.Item.ItemIndex)) )
'Update event:
Try
cmd.Connection.Open
cmd.ExecuteNonQuery
Catch
lblFeedBack.Text = "(UPDATE EVENT)Error: #" & _
Err.Number & "< br>Description: " & _
Err.Description & "< br>" & sql & "< br>"
lblFeedBack.Visible = True
Exit Sub
Finally
cmd.Connection.Close
End Try
rem *****************************************************
MY NEW CODE (no exceptions):
'Initialize variables:
Dim tb As TextBox, sql, strDate, strTitle As String
Dim strAbstract As String
Dim intID As Integer
'Assign field values to variables:
tb = e.Item.FindControl("txtDate")
strDate = tb.Text
tb = e.Item.FindControl("txtTitle")
strTitle = Replace( tb.Text, "'", "''" )
tb = e.Item.FindControl("txtAbstract")
strAbstract = Replace( tb.Text, "'", "''" )
intID = EventGrid.DataKeys(CInt(e.Item.ItemIndex))
sql = "UPDATE tblEvents SET eveDate=#" & strDate & _
"#, eveTitle='" & strTitle & _
"', eveAbstract='" & strAbstract & _
"' WHERE eveID=" & intID & ";"
Dim cmd As New OleDBCommand( sql, con )
cmd.CommandType = CommandType.Text
.....
and i replace a text field's value with an empty string
(by erasing the asp:textbox content and firing the
DataGrid's update command), my TRY block catches the
exception (Parameter @Abstract has no default value).
when i rewrote my code to refrain from using parameters i
received no error, and the update works flawlessly.
standard procedure would be to use parameters. how do i
get mine to work?
rem *****************************************************
MY ORIGINAL CODE (catches exception):
'Initialize variables:
Dim tb As TextBox
Dim sql As String = "UPDATE tblEvents " & _
"SET eveDate=@Date, eveTitle=@Title, " & _
"eveAbstract=@Abstract WHERE eveID=@intID;"
Dim cmd As New OleDBCommand( sql, con )
'Assign values to query parameters:
tb = e.Item.FindControl("txtDate")
cmd.Parameters.Add( "@Date", CDate(tb.Text) )
tb = e.Item.FindControl("txtTitle")
cmd.Parameters.Add( "@Title", Replace( tb.Text, _
"'", "''" ) )
tb = e.Item.FindControl("txtAbstract")
cmd.Parameters.Add( "@Abstract", Replace( tb.Text, _
"'", "''" ) )
cmd.Parameters.Add( "@intID", EventGrid.DataKeys( _
CInt(e.Item.ItemIndex)) )
'Update event:
Try
cmd.Connection.Open
cmd.ExecuteNonQuery
Catch
lblFeedBack.Text = "(UPDATE EVENT)Error: #" & _
Err.Number & "< br>Description: " & _
Err.Description & "< br>" & sql & "< br>"
lblFeedBack.Visible = True
Exit Sub
Finally
cmd.Connection.Close
End Try
rem *****************************************************
MY NEW CODE (no exceptions):
'Initialize variables:
Dim tb As TextBox, sql, strDate, strTitle As String
Dim strAbstract As String
Dim intID As Integer
'Assign field values to variables:
tb = e.Item.FindControl("txtDate")
strDate = tb.Text
tb = e.Item.FindControl("txtTitle")
strTitle = Replace( tb.Text, "'", "''" )
tb = e.Item.FindControl("txtAbstract")
strAbstract = Replace( tb.Text, "'", "''" )
intID = EventGrid.DataKeys(CInt(e.Item.ItemIndex))
sql = "UPDATE tblEvents SET eveDate=#" & strDate & _
"#, eveTitle='" & strTitle & _
"', eveAbstract='" & strAbstract & _
"' WHERE eveID=" & intID & ";"
Dim cmd As New OleDBCommand( sql, con )
cmd.CommandType = CommandType.Text
.....