Solved

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

Posted on 2012-04-03
8
230 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
[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
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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