JS List
asked on
ASP.Net VB Update Gridview DirectCast Object reference not set to an instance of an object.
Hi Experts,
Receiving an error, "Object reference not set to an instance of an object.", when try to use result on the 2nd DirectCast.
Using vb & asp.net.
The strange thing is if I uncomment the rest of the DirectCast & from
'Using con
' Using cmd As New OleDbCommand(strSQL, con)
on down. The error happens and the database gets updated.
Here's the front page:
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
Here's to Code Behind:
Any ideas?
JS
Receiving an error, "Object reference not set to an instance of an object.", when try to use result on the 2nd DirectCast.
Using vb & asp.net.
The strange thing is if I uncomment the rest of the DirectCast & from
'Using con
' Using cmd As New OleDbCommand(strSQL, con)
on down. The error happens and the database gets updated.
Here's the front page:
~~~~~~~~~~~~~~~~~~~~~~~~~
<asp:GridView ID="gvOrderDetails" runat="server" AutoGenerateColumns="False"
DataKeyNames="Order_Details_ID"
Width="745px" BorderWidth="1px"
OnRowCancelingEdit="gvOrderDetails_RowCancelingEdit"
OnRowDeleting="gvOrderDetails_RowDeleting"
OnRowEditing="gvOrderDetails_RowEditing"
OnRowUpdating="gvOrderDetails_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="Order_Details_ID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("Order_Details_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle CssClass="text" Width="50px" />
<HeaderStyle CssClass="textBold" Width="50px" />
<ItemStyle CssClass="text" HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product" SortExpression="Product_ID">
<ItemTemplate>
<asp:Label ID="lblProd" runat="server" Text='<%# Bind("Product_ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProd" runat="server" Text='<%# Bind("Product_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle CssClass="text" Width="150px" />
<HeaderStyle CssClass="textBold" Width="150px" />
<ItemStyle CssClass="text" Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Year" SortExpression="Product_Year">
<ItemTemplate>
<asp:Label ID="lblYear" runat="server" Text='<%# Bind("Product_Year") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtYear" runat="server" Text='<%# Bind("Product_Year") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle CssClass="text" Width="50px" />
<HeaderStyle CssClass="textBold" />
<ItemStyle CssClass="text" HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price" SortExpression="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price", "{0:0.00}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPrice" runat="server"
Text='<%# Bind("Price", "{0:0.00}") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle CssClass="text" Width="75px" />
<HeaderStyle CssClass="textBold" />
<ItemStyle CssClass="text" HorizontalAlign="Right" Width="75px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Cost" SortExpression="Item_Total">
<ItemTemplate>
<asp:Label ID="lblCost" runat="server"
Text='<%# Bind("Item_Total", "{0:0.00}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCost" runat="server"
Text='<%# Bind("Item_Total", "{0:0.00}") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle CssClass="text" Width="100px" />
<HeaderStyle CssClass="textBold" />
<ItemStyle CssClass="text" HorizontalAlign="Right" Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Ship Date" SortExpression="Item_Shipped">
<ItemTemplate>
<asp:Label ID="lblShip" runat="server"
Text='<%# Bind("Item_Shipped", "{0:M/d/yyyy}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtShip" runat="server"
Text='<%# Bind("Item_Shipped", "{0:M/d/yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle CssClass="text" Width="100px" />
<HeaderStyle CssClass="textBold" />
<ItemStyle CssClass="text" HorizontalAlign="Center" Width="100px" />
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True"
ShowEditButton="True" ShowDeleteButton="True"
ButtonType="Button" ShowSelectButton="False" />
</Columns>
<EditRowStyle HorizontalAlign="Left" />
<HeaderStyle BackColor="#E7EFF7" HorizontalAlign="Left" />
<RowStyle HorizontalAlign="Left" />
<SelectedRowStyle HorizontalAlign="Left" />
</asp:GridView>
~~~~~~~~~~~~~~~~~~~~~~~~~~
Here's to Code Behind:
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Imports States_All
Imports VMOS
Partial Class VMOS_OrderHistory
Inherits System.Web.UI.Page
Public thisID As String
Public ds As DataSet
Public row As DataRow
Public orderID As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim thisPage As String = Request.Url.ToString 'Check Security
If (checkSession(thisPage) = "False") Then
Response.Redirect("../indexTools.aspx")
End If
'~~~~~~~~~~~~~~~ ORDER INFORMATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
thisID = Request.QueryString("j") 'Contains Order#
If (Not IsPostBack) Then
bind()
End If
End Sub
Protected Sub gvOrderDetails_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvOrderDetails.RowUpdating
Dim con As New OleDbConnection
Dim row As GridViewRow = DirectCast(gvOrderDetails.Rows(e.RowIndex), GridViewRow)
Dim idLbl As New Label
idLbl = DirectCast(row.Cells(0).FindControl("lblID"), Label)
lbl8.text = idLbl.text '~~~~~~~~~~~~~ This Works
Dim qtyTxt As New TextBox
[b]qtyTxt = DirectCast(row.Cells(1).FindControl("txtQty"), TextBox) '~~~~~~~~~~ Blows up Here[/b]
'Dim prodTxt As New TextBox
'prodTxt = DirectCast(row.Cells(2).FindControl("txtProd"), TextBox)
'Dim yearTxt As New TextBox
'yearTxt = DirectCast(row.Cells(3).FindControl("txtYear"), TextBox)
'Dim priceTxt As New TextBox
'priceTxt = DirectCast(row.Cells(4).FindControl("txtPrice"), TextBox)
'Dim costTxt As New TextBox
'costTxt = DirectCast(row.Cells(5).FindControl("txtCost"), TextBox)
'Dim shipTxt As New TextBox
'shipTxt = DirectCast(row.Cells(6).FindControl("txtShip"), TextBox)
Dim strSQL As String
strSQL = "Update Order_Details SET "
strSQL = strSQL + "Product_ID = ?, Quantity = ?, Price = ?, Product_Year = ?, Item_Total = ?, Item_Shipped = ? "
strSQL = strSQL + "WHERE (Order_Details_ID = ?)"
con = addData()
Dim cmd As New OleDbCommand
cmd = New OleDbCommand(strSQL, con)
'Using con
' Using cmd As New OleDbCommand(strSQL, con)
' cmd.CommandType = CommandType.Text
' cmd.Parameters.AddWithValue("Product_ID", prodTxt.Text)
' cmd.Parameters.AddWithValue("Quantity", qtyTxt.Text)
' cmd.Parameters.AddWithValue("Price", priceTxt.Text)
' cmd.Parameters.AddWithValue("Product_Year", yearTxt.Text)
' cmd.Parameters.AddWithValue("Item_Total", costTxt.Text)
' cmd.Parameters.AddWithValue("Item_Shipped", shipTxt.Text)
' cmd.Parameters.AddWithValue("Order_Details_ID", idLbl.Text)
' con.Open()
' cmd.ExecuteNonQuery()
' End Using
'End Using
gvOrderDetails.EditIndex = -1
bind()
End Sub
Sub bind()
Dim strSQL As String
strSQL = "SELECT * FROM Order_Details "
strSQL = strSQL + "WHERE Order_ID = " & thisID & " "
strSQL = strSQL + "ORDER BY Order_Details_ID"
ds = GetData(strSQL)
gvOrderDetails.DataSource = ds
gvOrderDetails.DataBind()
End Sub
Any ideas?
JS
Try using the Label in the item template as your base type, and cast to a Label object just to see what happens. I say this because, as you point out, your first field cast works - you may no longer be in edit mode when the row is updating.
lblID is part of the ItemTemplate.
txtQty is part of the EditItemTemplate.
txtQty is part of the EditItemTemplate.
BTW, that suggestion hasn't been tested by me, and is in the nature of a guess.
ASKER
I tried the label choice and no luck. The lbl that works is actually a label in the form that's hidden - it's the order-detail id #.
Item Template vs EditItemTemplate -
After the user enters a change value, in the edit text box - then it goes to the RowUpdating sub. At that point should I call the EditItemTemplate or the ItemTemplate to get the change?
Item Template vs EditItemTemplate -
After the user enters a change value, in the edit text box - then it goes to the RowUpdating sub. At that point should I call the EditItemTemplate or the ItemTemplate to get the change?
I would try the objects in the EditItemTemplate. This is where the items edited should be.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
LOL next time I'll ask, "Did you create it in a wizard? Can you recreate it?" Good catch.
ASKER
While the information was helpful there was no clear answer submitted. I recreated it with the wizard.