Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

NullReferenceException

Posted on 2007-11-28
25
267 Views
Last Modified: 2008-03-10
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
Comment
Question by:QPR
  • 14
  • 11
25 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20370646
Hi, what line is the error happening on?
0
 
LVL 29

Author Comment

by:QPR
ID: 20370710
 objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = CType(GridView1.Rows(e.RowIndex).Cells(3).FindControl("StartDate"), TextBox).Text
0
 
LVL 22

Accepted Solution

by:
RedKelvin earned 500 total points
ID: 20370798
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Author Comment

by:QPR
ID: 20370851
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
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20370868
That should be straight forward, you have a string, that is not in a recognizable date format, it may be null or empty
0
 
LVL 29

Author Comment

by:QPR
ID: 20370875
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
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20370907
Then I would say the issue is actually in your stored procedure, and not the input
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20370910
You may also want to make use of this
http://msdn2.microsoft.com/en-us/library/ch92fbc1.aspx
0
 
LVL 29

Author Comment

by:QPR
ID: 20370917
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
 
LVL 29

Author Comment

by:QPR
ID: 20370923
objCmd.Parameters.Add("@startDate", Data.SqlDbType.DateTime).Value = DateTime.Parse(GridView1.Rows(e.RowIndex).Cells(3).Text)

same error
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20370925
in your code, parse the date strings to datetime, before passing them in
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20370932
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
 
LVL 29

Author Comment

by:QPR
ID: 20370942
This line gives the error
Dim dteDate As DateTime = DateTime.Parse(GridView1.Rows(e.RowIndex).Cells(3).Text)

thanks for sticking with it
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20370958
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
 
LVL 29

Author Comment

by:QPR
ID: 20370977
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
 
LVL 29

Author Comment

by:QPR
ID: 20370983
but startdate is cells(3) assuming it's a zero based count?
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20371012
The commmandfield may not be counted
0
 
LVL 29

Author Comment

by:QPR
ID: 20371027
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
 
LVL 29

Author Comment

by:QPR
ID: 20371050
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
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20371071
try using <asp:boundcolumn instead fo <asp:boundfield

Is the data displaying in your gridview?
0
 
LVL 29

Author Comment

by:QPR
ID: 20371085
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
 
LVL 29

Author Comment

by:QPR
ID: 20371113
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
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20371125
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
 
LVL 29

Author Comment

by:QPR
ID: 20376605
       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
 
LVL 29

Author Comment

by:QPR
ID: 20377538
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why does my excel data start at B1 instead of A1. 9 93
Regex validation 2 28
asp enable view state 1 28
C# LINQ ForEach() question 6 54
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question