Well what i have is a Datagrid that ,Updates,Adds and Delete.
When i add i have no error but when i click Update and Delete commands it
fires an error..
The Error line is at :-
"ItemNumber = CType(Args.Item .FindControl("I temNumber"), TextBox).Text"
Any ideas?
My code is below(its my EditRecord Sub porgram):-
Sub EditRecord(ByVa l Src As Object, ByVal Args As DataGridCommand EventArgs)
Dim Command As String
Dim ItemNumber As String
Dim ItemType As String
Dim ItemSupplier As String
Dim ItemName As String
Dim ItemDescription As String
Dim ItemPrice As String
Dim ItemQuantity As String
Dim DBConnection As SqlConnection
Dim DBCommand As SqlCommand
Dim DBReader As SqlDataReader
Dim SQLString As String
Command = Args.CommandSou rce.Text
ItemNumber = CType(Args.Item .FindControl("I temNumber"), TextBox).Text
ItemType = CType(Args.Item .FindControl("I temType"),
DropDownList).S electedItem.Tex t
ItemSupplier = CType(Args.Item .FindControl("I temSupplier"),
TextBox).Text
ItemName = CType(Args.Item .FindControl("I temName"), TextBox).Text
ItemDescription = CType(Args.Item .FindControl("I temDescription" ),
TextBox).Text
ItemPrice = CType(Args.Item .FindControl("I temPrice"), TextBox).Text
ItemQuantity = CType(Args.Item .FindControl("I temQuantity"),
TextBox).Text
UpdateMessage.T ext = " "
Dim ValidRecord As Boolean = True
If Command = "Add" Or Command = "Update" Then
'-- CHECK FOR VALID RECORD ---
'-- Check for valid ItemNumber
If Len(ItemNumber) <> 6 Then
UpdateMessage.T ext &= "- Invalid Item Number length "
ValidRecord = False
ElseIf Not IsNumeric(Right (ItemNumber, 4)) Then
UpdateMessage.T ext &= "- Invalid Item Number format "
ValidRecord = False
Else
ItemNumber = UCase(ItemNumbe r)
End If
'-- Check for missing Item Supplier
If ItemSupplier = "" Then
UpdateMessage.T ext &= "- Missing Supplier "
ValidRecord = False
End If
'-- Check for missing Item Name
If ItemName = "" Then
UpdateMessage.T ext &= "- Missing Name "
ValidRecord = False
End If
'-- Check for missing Item Description
If ItemDescription = "" Then
UpdateMessage.T ext &= "- Missing Description "
ValidRecord = False
End If
'-- Check for valid Item Price
If Not IsNumeric(ItemP rice) Then
UpdateMessage.T ext &= "- Invalid Price format "
ValidRecord = False
End If
'-- Check for valid Item Quantity
If Not IsNumeric(ItemQ uantity) Then
UpdateMessage.T ext &= "- Invalid Quantity format "
ValidRecord = False
End If
If ValidRecord = True And Command = "Add" Then
'--- CHECK FOR DUPLICATE RECORD ---
DBConnection = New
SqlConnection(" server=(local); database=Northw ind;integrated security=true;" )
DBConnection.Op en()
SQLString = "SELECT Count(*) FROM products1 WHERE ItemNumber
= '" & ItemNumber & "'"
DBCommand = New SqlCommand(SQLS tring, DBConnection)
If DBCommand.Execu teScalar() <> 0 Then
UpdateMessage.T ext = "- Duplicate Item Number. Record
not added."
ValidRecord = False
End If
DBConnection.Cl ose()
End If
End If
If ValidRecord = True Then
Select Case Command
Case "Add"
SQLString = "INSERT INTO products1 " & _
"(ItemNumbe r, ItemType, ItemSupplier, ItemName, " & _
"ItemDescriptio n, ItemPrice, ItemQuantity) VALUES(" & _
"'" & ItemNumber & "', " & _
"'" & ItemType & "', " & _
"'" & Replace(ItemSup plier, "'", "''") & "', " & _
"'" & Replace(ItemNam e, "'", "''") & "', " & _
"'" & Replace(ItemDes cription, "'", "''") & "', " & _
ItemPrice & ", " & _
ItemQuantity & ")"
UpdateMessage.T ext = "- Record " & ItemNumber & " added"
Case "Update"
SQLString = "UPDATE products1 SET " & _
"ItemType = '" & ItemType & "', " & _
"ItemSuppli er = '" & Replace(ItemSup plier, "'", "''")
& "', " & _
"ItemName = '" & Replace(ItemNam e, "'", "''") & "', "
& _
"ItemDescriptio n = '" & Replace(ItemDes cription, "'",
"''") & "', " & _
"ItemPrice = " & ItemPrice & ", " & _
"ItemQuanti ty = " & ItemQuantity & " " & _
"WHERE ItemNumber = '" & ItemNumber & "'"
UpdateMessage.T ext = "- Record " & ItemNumber & " updated"
Case "Delete"
SQLString = "DELETE FROM products1 WHERE ItemNumber = '"
& ItemNumber & "'"
UpdateMessage.T ext = "- Record " & ItemNumber & " deleted"
End Select
DBConnection = New
SqlConnection(" server=(local); database=Northw ind;integrated security=true;" )
DBConnection.Op en()
DBCommand = New SqlCommand(SQLS tring, DBConnection)
DBCommand.Execu teNonQuery()
DBConnection.Cl ose()
If Command = "Add" Or Command = "Delete" Then
ViewState("Upda ted") = True
Page_Load(Me, EventArgs.Empty )
'Page_Load(ByRe f sender As System.Object, ByRef e As
System.EventArg s)
End If
End If
End Sub
"Karl Seguin" wrote:
If you showed us some code, we'd probably be more likely to point out the
exact error..especial ly if you could provide a line #
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Patrick.O. Ige" <Pa*********@di scussions.micro soft.com> wrote in message
news:EE******** *************** ***********@mic rosoft.com... What could cause the error:-
System.NullRefe renceException: Object reference not set to an instance of
an object.
Any ideas?