Solved

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

Posted on 2012-04-03
8
204 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
 

Author Comment

by:Amien90
ID: 37800619
ddlProducts.DataSource = cmd.ExecuteNonQuery()
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now