Update Record Error

Results 1 to 2 of 2

Thread: Update Record Error

  1. #1
    Join Date
    Dec 1969

    Default Update Record Error

    Hi everyone,<BR>Please look at the code below and help me point out the reason why I keep getting this error:<BR>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <BR><BR>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.<BR>Line 84: MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NVarChar, 50))<BR><BR>Code:<BR><BR>&#060;asp:TemplateColumn HeaderText="Phone" SortExpression="LocationPhone"&#062;<BR>&#060;Item Template&#062;<BR> &#060;asp:Label runat="server" Text=&#039;&#060;%# DataBinder.Eval(Container.DataItem, "LocationPhone") %&#062;&#039; ID="Label9"/&#062;<BR> &#060;/ItemTemplate&#062;<BR> &#060;EditItemTemplate&#062;<BR> &#060;asp:TextBox runat="server" id="edit_Phone" Text=&#039;&#060;%# DataBinder.Eval(Container.DataItem, "LocationPhone") %&#062;&#039;/&#062;<BR> &#060;asp:RegularExpressionValidator id="phoneRegexVal" ControlToValidate="edit_Phone" ValidationExpression="[0-9]{3} [0-9]{3}-[0-9]{4}" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server"&#062;<BR> * Phone must be in form: XXX XXX-XXXX <BR><BR> &#060;/asp:RegularExpressionValidator&#062;<BR> &#060;/EditItemTemplate&#062;<BR> &#060;/asp:TemplateColumn&#062;<BR><BR>....<BR>MyDataGrid _update(ByVal sender As Object, ByVal E As DataGridCommandEventArgs)<BR> If Page.IsValid Then<BR> Dim DS As DataSet<BR> Dim MyCommand As SqlCommand<BR> Dim UpdateCmd As String = "UPDATE into Locations SET LocationID = @Id, LocationName=@Name,LocationArea=@Area," _<BR> & "LocationAddress=@Add,LocationCity=@City,LocationS tate=@State,LocationZip=@Zip," _<BR> & "LocationContact=@Contact,LocationPhone=@Phone ," _<BR> & "LocationDirections=@Dir,LocationNotes=@Notes where LocationID = @Id "<BR><BR> MyCommand = New SqlCommand(UpdateCmd, MyConnection)<BR> MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.Int))<BR> MyCommand.Parameters.Add(New SqlParameter("@Name", SqlDbType.NVarChar, 50))<BR> MyCommand.Parameters.Add(New SqlParameter("@Area", SqlDbType.NVarChar, 50))<BR> MyCommand.Parameters.Add(New SqlParameter("@Add", SqlDbType.NVarChar, 50))<BR> MyCommand.Parameters.Add(New SqlParameter("@City", SqlDbType.NVarChar, 50))<BR> MyCommand.Parameters.Add(New SqlParameter("@state", SqlDbType.NVarChar, 2))<BR> MyCommand.Parameters.Add(New SqlParameter("@Zip", SqlDbType.NVarChar, 10))<BR> MyCommand.Parameters.Add(New SqlParameter("@Contact", SqlDbType.NVarChar, 50))<BR> MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NVarChar, 50))<BR> MyCommand.Parameters.Add(New SqlParameter("@Dir", SqlDbType.NVarChar, 255))<BR> MyCommand.Parameters.Add(New SqlParameter("@Notes", SqlDbType.NVarChar, 255))<BR><BR> MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))<BR> Dim cols As String() = {"Name", "Area", "Add", "City", "Zip", "Contact", "Phone", "Dir", "Notes"}<BR> <BR> Dim I As Integer<BR> For I = 0 To 8<BR> Dim CurrentTextBox As TextBox<BR> CurrentTextBox = E.Item.FindControl("edit_" & cols(I))<BR> Dim ColValue As String = CurrentTextBox.Text<BR> MyCommand.Parameters("@" & cols(I)).Value = Server.HtmlEncode(ColValue)<BR><BR> Next<BR>

  2. #2
    Join Date
    Dec 1969

    Default your loop

    if i understand your code, it seems like you are using your I = 0 to 8 loop to populate the values for your parameters. that means your problem is in the process of iterating through the parameters, and that the process is breaking down specifically at the point ofthe @Phone parameter.<BR><BR>therefore, you should test your loop to see what it is outputting. when you get the loop to output the proper values independently of hooking them up to these parameters, you will most likely have found your error and the script should work when you hook the parameters back up to the loop.<BR><BR>does this help? if it does, i would appreciate some help on the problem i am currently working on, which you can see at:<BR>http://www.aspmessageboard.com/forum/aspplus.asp?M=521686&T=521686&F=36&P=1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts