• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

NullReferenceException

Not sure why I'm getting this...
All fields are populated, it points to the second line in my parameter list

Gridview columns followed by code behind:

<Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="Edit" ImageAlign="AbsMiddle"  OnClientClick="return confirm('Are you sure you want to delete this vacancy?');"
                        ImageUrl="~/Admin/images/delete.gif" CommandName='<%# eval("id") %>' OnClick="ImageButton1_Click" />
                </ItemTemplate>
                <ItemStyle Width="30px" />
            </asp:TemplateField>
            <asp:CommandField ButtonType="Button" DeleteText="" ShowEditButton="True" >
                <ItemStyle Width="75px" />
            </asp:CommandField>
            <asp:BoundField DataField="JobTitle" HeaderText="Job Title" SortExpression="JobTitle" />
            <asp:BoundField DataField="StartDate" HeaderText="Start Date" SortExpression="StartDate">
                <ItemStyle BorderStyle="None" BorderWidth="0px" Width="100px" />
            </asp:BoundField>
            <asp:BoundField DataField="ExpireDate" HeaderText="Close Date" SortExpression="ExpireDate">
                <ItemStyle BorderStyle="None" BorderWidth="0px" Width="100px" />
            </asp:BoundField>
            <asp:TemplateField HeaderText="Link2Doc" SortExpression="Link2Doc">
                <EditItemTemplate>
                    <asp:FileUpload ID="Link2Doc" runat="server"  />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Link2Doc") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField Visible="False">
                <ItemTemplate>
                    <asp:Label ID="OriginalDoc" runat="server" Text='<%# Eval("Link2Doc") %>' Visible="False"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
                <ItemTemplate>
                    <asp:Label ID="id" runat="server" Visible="False" Text='<%# Bind("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim docPath As String = Server.MapPath("~/HR/InternalVacancies/JDs/")
        Dim strOriginalFile As String = docPath & CType(GridView1.Rows(e.RowIndex).Cells(6).FindControl("OriginalDoc"), Label).Text

        Dim filePath As String = docPath & CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("Link2Doc"), FileUpload).FileName

        Dim upload As FileUpload = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("Link2Doc"), FileUpload)
        If CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("Link2Doc"), FileUpload).FileName = "" Then
            filePath = strOriginalFile
        Else
            File.Delete(strOriginalFile)
            upload.SaveAs(filePath)
        End If
        Dim connectionstring As String = System.Configuration.ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString
        Dim ObjConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionstring)
        Dim objCmd As SqlCommand = New SqlCommand()
        objCmd.Connection = ObjConn
        objCmd.CommandType = System.Data.CommandType.StoredProcedure
        objCmd.CommandText = "dbo.UpdateInternalVacancies"
        objCmd.Parameters.Add("@id", Data.SqlDbType.Int).Value = CType(GridView1.Rows(e.RowIndex).Cells(7).FindControl("id"), Label).Text
        objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = CType(GridView1.Rows(e.RowIndex).Cells(3).FindControl("StartDate"), TextBox).Text
        objCmd.Parameters.Add("@ExpireDate", Data.SqlDbType.DateTime).Value = CType(GridView1.Rows(e.RowIndex).Cells(4).FindControl("ExpireDate"), TextBox).Text
        objCmd.Parameters.Add("@jobTitle", Data.SqlDbType.VarChar).Value = CType(GridView1.Rows(e.RowIndex).Cells(2).FindControl("JobTitle"), TextBox).Text
        objCmd.Parameters.Add("@Link2Doc", Data.SqlDbType.VarChar).Value = filePath

        ObjConn.Open()
        objCmd.ExecuteNonQuery()
        ObjConn.Close()
        System.Web.HttpContext.Current.Response.Redirect("default.aspx")
    End Sub
End Class
0
QPR
Asked:
QPR
  • 14
  • 11
1 Solution
 
RedKelvinCommented:
Hi, what line is the error happening on?
0
 
QPRAuthor Commented:
 objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = CType(GridView1.Rows(e.RowIndex).Cells(3).FindControl("StartDate"), TextBox).Text
0
 
RedKelvinCommented:
Ok, the problem seems to be that you are using findcontrol on a boundcolumn, not a templateItem, like most of your other columns, this means that there is no control to find, it is just a cell, you will need to reference it something like this, or turn it into a template column.


objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = gridview1.Rows(e.RowIndex).Cells(3).Text

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
QPRAuthor Commented:
thanks, I've got past that error and now have a new one for objCmd.ExecuteNonQuery()
Failed to convert parameter value from a String to a DateTime.
0
 
RedKelvinCommented:
That should be straight forward, you have a string, that is not in a recognizable date format, it may be null or empty
0
 
QPRAuthor Commented:
Start Date is 22/11/2007
End Date is 29/11/2007
Both dd/mm/yyyy

The grid view is populated from a table and these 2 values are stored in datetime columns.
0
 
RedKelvinCommented:
Then I would say the issue is actually in your stored procedure, and not the input
0
 
RedKelvinCommented:
You may also want to make use of this
http://msdn2.microsoft.com/en-us/library/ch92fbc1.aspx
0
 
QPRAuthor Commented:
ALTER PROCEDURE [dbo].[UpdateInternalVacancies]
@id int,
@StartDate datetime,
@ExpireDate datetime,
@JobTitle varchar(150),
@link2doc varchar(150)
AS
BEGIN
update dbo.InternalVacancies
set StartDate = @StartDate, ExpireDate = @ExpireDate, JobTitle = @JobTitle, Link2Doc = @Link2Doc
where id = @id
END
0
 
QPRAuthor Commented:
objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = DateTime.Parse(GridView1.Rows(e.RowIndex).Cells(3).Text)

same error
0
 
RedKelvinCommented:
in your code, parse the date strings to datetime, before passing them in
0
 
RedKelvinCommented:
objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = DateTime.Parse(GridView1.Rows(e.RowIndex).Cells(3).Text)


split this onto 2 lines to find out which line is causing the error

dim dteDate as DateTime = DateTime.Parse(GridView1.Rows(e.RowIndex).Cells(3).Text)
objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = dteDate
0
 
QPRAuthor Commented:
This line gives the error
Dim dteDate As DateTime = DateTime.Parse(GridView1.Rows(e.RowIndex).Cells(3).Text)

thanks for sticking with it
0
 
RedKelvinCommented:
no worries,

Ok so the problem is in the VB, not the stored proc, it appears that GridView1.Rows(e.RowIndex).Cells(3).Text
is not outputting a date string, can you output that string to see exactly what is in it
0
 
QPRAuthor Commented:
Interesting..
MsgBox(GridView1.Rows(e.RowIndex).Cells(3).Text)
gives me an empty messagebox.
Perhaps I haven't lined up my columns and cells(X) correctly?
0
 
QPRAuthor Commented:
but startdate is cells(3) assuming it's a zero based count?
0
 
RedKelvinCommented:
The commmandfield may not be counted
0
 
QPRAuthor Commented:
I've gone through all of them (msgbox) and the only value I can get is
GridView1.Rows(e.RowIndex).Cells(7).FindControl("id"), Label).Text
0
 
QPRAuthor Commented:
I've done similar in other places on this project but on other occasions I've just used the normal <update parameters> in the aspx file or I've been able to reference the field directly e.g.  cmd.Parameters.Add("@StartDate", Data.SqlDbType.DateTime).Value = StartDate as they've been in individual controls rather than a gridview.
0
 
RedKelvinCommented:
try using <asp:boundcolumn instead fo <asp:boundfield

Is the data displaying in your gridview?
0
 
QPRAuthor Commented:
Error      1      System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:BoundColumn' is of type 'System.Web.UI.WebControls.BoundColumn'.

Don't understand that one.

Yes shows fine in gridview      
0
 
QPRAuthor Commented:
oh sh1t!
You gave me a clue there... I looked at the SP that populates the gridview and the start and date columns were selected as...
convert(startdate, varchar) as StartDate! no wonder it was only showing as dd/mm/yyyy
I've removed that and now I get past the add.parameter errors but I get the same error (converting string to datetime) on the objCmd.ExecuteNonQuery() line
0
 
RedKelvinCommented:
I would say that gridviews don't like bound columns

Check your column indexing, as if you try to find a control in the wrong column you will not get a value.
0
 
QPRAuthor Commented:
       Dim test1 As String = GridView1.Rows(e.RowIndex).Cells(1).Text
        Dim test2 As String = GridView1.Rows(e.RowIndex).Cells(2).Text
        Dim test3 As String = GridView1.Rows(e.RowIndex).Cells(3).Text
        Dim test4 As String = GridView1.Rows(e.RowIndex).Cells(4).Text
        Dim test5 As String = GridView1.Rows(e.RowIndex).Cells(5).Text
        Dim test6 As String = GridView1.Rows(e.RowIndex).Cells(6).Text
        Dim test7 As String = GridView1.Rows(e.RowIndex).Cells(7).Text

test1 - test5 return ""
test 6 and test7 return nothing
0
 
QPRAuthor Commented:
All solved!
Had to use a mixture of findcontrol and controls(0) depending on the column type to get it all to work.
Thanks for the help.

        objCmd.Parameters.Add("@jobTitle", Data.SqlDbType.VarChar).Value = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
        objCmd.Parameters.Add("@id", Data.SqlDbType.Int).Value = CType(GridView1.Rows(e.RowIndex).Cells(7).FindControl("id"), Label).Text
        objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
        objCmd.Parameters.Add("@ExpireDate", Data.SqlDbType.DateTime).Value = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 14
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now