Solved

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

Posted on 2012-04-03
8
223 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

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

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

697 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