?
Solved

why? "Object reference not set to an instance of an object."

Posted on 2012-04-03
8
Medium Priority
?
249 Views
Last Modified: 2012-04-03
i dont get it. Thanks in advanced

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound

        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDatabase").ConnectionString)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        Dim cmd As New OleDb.OleDbCommand("SELECT DISTINCT * FROM [Products]  ORDER BY ProductName", con)

        Dim ddlProducts As DropDownList = e.Row.Cells(13).FindControl("ddlProducts")

        ddlProducts.DataSource = cmd.ExecuteNonQuery()
        ddlProducts.DataValueField = "ProductID"
        ddlProducts.DataTextField = "ProductName"
        ddlProducts.DataBind()

Open in new window

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound

        'eerst productddl en accountddl vullen

        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDatabase").ConnectionString)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        Dim cmd As New OleDb.OleDbCommand("SELECT DISTINCT * FROM [Products]  ORDER BY ProductName", con)

        Dim ddlProducts As DropDownList = e.Row.Cells(13).FindControl("ddlProducts")

        ddlProducts.DataSource = cmd.ExecuteNonQuery()
        ddlProducts.DataValueField = "ProductID"
        ddlProducts.DataTextField = "ProductName"
        ddlProducts.DataBind()

Open in new window

0
Comment
Question by:Amien90
  • 3
  • 3
  • 2
8 Comments
 
LVL 20

Expert Comment

by:a_b
ID: 37800562
The object that you are trying to use has a value null. It has not been initialized as yet by using the new keyword.

Post the full stack trace.
0
 

Author Comment

by:Amien90
ID: 37800580
System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=App_Web_gp3snl3k
  StackTrace:
       at _Default.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\Users\molivier\Documents\Visual Studio 2010\WebSites\BulkLoader\Default.aspx.vb:line 1162
       at System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e)
       at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
       at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
       at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
       at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
       at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
       at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
       at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at System.Web.UI.WebControls.GridView.DataBind()
       at _Default.AddNewRowToGrid() in C:\Users\molivier\Documents\Visual Studio 2010\WebSites\BulkLoader\Default.aspx.vb:line 100
       at _Default.Button1_Click(Object sender, EventArgs e) in C:\Users\molivier\Documents\Visual Studio 2010\WebSites\BulkLoader\Default.aspx.vb:line 119
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
0
 
LVL 20

Expert Comment

by:a_b
ID: 37800610
What is at Default.aspx.vb:line 1162?
0
Independent Software Vendors: 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!

 

Author Comment

by:Amien90
ID: 37800619
ddlProducts.DataSource = cmd.ExecuteNonQuery()
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 37800621
Try like this

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound

        'eerst productddl en accountddl vullen

        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDatabase").ConnectionString)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        Dim cmd As New OleDb.OleDbCommand("SELECT DISTINCT * FROM [Products]  ORDER BY ProductName", con)

       If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ddlProducts As DropDownList = CType(e.Row.FindControl("ddlProducts"), DropDownList)

        ddlProducts.DataSource = cmd.ExecuteNonQuery()
        ddlProducts.DataValueField = "ProductID"
        ddlProducts.DataTextField = "ProductName"
        ddlProducts.DataBind()

   End If
0
 

Author Comment

by:Amien90
ID: 37800637
@sonawanekiran, this will result in

Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource.
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 37800661
You need to catch the result for query into dataset/datatable/list  then assign it to dropdown list. You can not do like

cmd.ExecuteNonQuery()
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 2000 total points
ID: 37800681
For instance,

 Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDatabase").ConnectionString)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        Dim cmd As New OleDb.OleDbCommand("SELECT DISTINCT * FROM [Products]  ORDER BY ProductName", con)



Dim da As New OleDbDataAdapter(cmd)

Dim ds As New DataSet()
da.Fill(ds)

ddlProducts.DataTextField = ds.Tables(0).Columns("ProductID").ToString()
ddlProducts.DataValueField = ds.Tables(0).Columns("ProductName").ToString()

ddlProducts.DataSource = ds.Tables(0)
ddlProducts.DataBind()
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 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