Hello guys,
Can anyone tell me why do I get the error message in the code below?

The code fires onPageLoad.

What should I change?

Dim con As New SqlConnection
con.ConnectionString = "Data Source=s01.winhost.com;Initial Catalog=DB_10078_kdn;Persist Security Info=True;User ID=xxxxx;Password=******"
        Dim s As String
        s = "SELECT * FROM Users WHERE Seller_id LIKE '" & Label5.Text & "'"
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(s, con)

        Try
            da.Fill(ds)
        Catch ex As Exception
        End Try

        DropDownList1.DataSource = ds
        DropDownList1.DataBind()
        DropDownList1.DataValueField = "Seller_id"
        DropDownList1.DataTextField = "Account_Type"

        Label7.Text = DropDownList1.SelectedItem.Text

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Recommended Answers

All 3 Replies

I believe this line should be after assigning DataTextField and DataValueField.

DropDownList1.DataBind()

so your code will be

DropDownList1.DataSource = ds
DropDownList1.DataValueField = "Seller_id"
DropDownList1.DataTextField = "Account_Type"
DropDownList1.DataBind()

try this !

Hello guys,
Can anyone tell me why do I get the error message in the code below?

The code fires onPageLoad.

What should I change?

Dim con As New SqlConnection
con.ConnectionString = "Data Source=s01.winhost.com;Initial Catalog=DB_10078_kdn;Persist Security Info=True;User ID=xxxxx;Password=******"
        Dim s As String
        s = "SELECT * FROM Users WHERE Seller_id LIKE '" & Label5.Text & "'"
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(s, con)

        Try
            da.Fill(ds)
        Catch ex As Exception
        End Try

        DropDownList1.DataSource = ds
        DropDownList1.DataBind()
        DropDownList1.DataValueField = "Seller_id"
        DropDownList1.DataTextField = "Account_Type"

        Label7.Text = DropDownList1.SelectedItem.Text

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I was recently having the exact same problem with a listbox control on my site.

The problem, as it turns out, was that the information bindings were being constantly changed every time the page was loaded (postback or otherwise).

I believe that if you change it so that the actual databind process is accomplished within the vb equivalent of below, it might work out for you:

if (Page.IsPostBack == false)
{
    ...page's databind processes here...
}

For example on my site's journal view page my Page_Load segment looks like this:

protected void Page_Load(object sender, EventArgs e)
    {
        //Split query pos 1 = Post Number
        string getQuery = ClientQueryString; //obtain variables from passed link ?orig=#&resp=#
        string[] querySplit = getQuery.Split('=', '&');
        origNum = Convert.ToInt16(querySplit[1].ToString());
        listedPage.Text = Convert.ToString(origNum);
        if (Page.IsPostBack == false)
        {
            loadMessageList();
            loadRespList(origNum);
        }
        navButtonSetup();
        if (origNum != -1) //if passed replyID != -1 load passed ID
        {
            loadMessage(origNum);
        }
        else //if passed replyID == -1 load lowest ID
        {
            origNum = lowestPost;
            loadMessage(lowestPost);
            if (Page.IsPostBack == false)
            {
                loadRespList(lowestPost);
            }
            navButtonSetup();
        }
        if (listRespBox.Items.Count > 0)
        {
            listRespButton.Visible = true;
        }
    }

However if I did the loadRespList or loadMessageList segments outside of the if (Page.IsPostBack == false) constructs I would receive the same error you reported with your code.

I hope this helps! :D Mark as solved if it does.

Great :)

Thank You Lusiphur - Solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.