Solved

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

Posted on 2012-04-03
8
208 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Authentication of Web Services 3 46
Error in page 3 44
Connection String 16 42
Help with C#, MVC, razor. 6 21
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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A short film showing how OnPage and Connectwise integration works.

919 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

16 Experts available now in Live!

Get 1:1 Help Now