0

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.

3
Contributors
3
Replies
7
Views
7 Years
Discussion Span
Last Post by slices
0

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.

1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.