?
Solved

NullReferenceException

Posted on 2007-11-28
25
Medium Priority
?
270 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
[X]
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
  • 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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