views:

66

answers:

2

When I choose am item from "kategorije", new items are loaded into "SUB_kategorije", but when I choose the item from SUB_kategorije and when i click on button it shows me this error:

Object reference not set to an instance of an object.

Line 101: kom.Parameters.Add("@podkategorija", SqlDbType.Text).Value =
          SUB_kategorije.SelectedItem.ToString();

This is my source...

dod_pit.ascx

dod_pit.ascx.cs

A: 

The problem is your Page_Load. Change it to:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlDataSource df = new SqlDataSource();
        df.ConnectionString = conn;
        df.SelectCommand = "SELECT [ID], [Kategorije] FROM [kategorije] ";
        kategorije.DataSource = df;
        kategorije.DataTextField = "Kategorije";
        kategorije.DataValueField = "ID";
        kategorije.DataBind();
    }         
}

The problem is your rebinding kategorije every time your code does a postback so by the time Button1_Click runs, kategorije has been rebinding and reset so the SelectedValue is null.

When a postback happens your Page_Load code runs first, then the Button1_Click runs right after. Moving that code into a !IsPostBack check will cause it to only run the first time your page loads and not again after so the SelectedValue will be available.

Side note: You should also improve your accepted answer rate... you have asked a bunch of question and not marked any as the accepted answer. If you improve your accept rate (which is 0%) and people will be more likely to help you out. Please review the FAQ and specifically the section: How do I ask questions here?

Kelsey
I have new problem why?
lodun
@Iodun You will need to provide more information as to what exactly is happening and what you expect to happen. Looking at your aspx code I don't understand why your SUB `ListBox` has `AppendDataBoundItems="true"` set.
Kelsey
I want to add posts(question) like i do that on yahoo answers.When i choose item in "kategorije" control,items can't be loaded in "SUB_kategorije" control..
lodun
This is my problem:http://img28.imageshack.us/img28/6100/listbox.jpg
lodun
A: 

When I choose am item from "kategorije", new items are not loaded into "SUB_kategorije",Why?

 protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {

            SqlDataSource ds = new SqlDataSource();
            ds.ConnectionString = conn;
            ds.SelectCommand = "SELECT [ID], [Kategorije] FROM [kategorije] ";
            kategorije.DataSource = ds;
            kategorije.DataTextField = "Kategorije";
            kategorije.DataValueField = "ID";
            kategorije.DataBind();
            kategorije.SelectedIndex = 1;

            SqlDataSource dk = new SqlDataSource();
            dk.ConnectionString = conn;
            dk.SelectCommand = "SELECT * from pod_kategorije WHERE kat_id = " + kategorije.SelectedItem.Value;
            SUB_kategorije.DataSource = dk;
            SUB_kategorije.DataTextField = "pkategorija";
            SUB_kategorije.DataValueField = "ID";
            SUB_kategorije.DataBind();


        }







    }
    protected void kategorije_SelectedIndexChanged(object sender, EventArgs e)
    {


            SqlDataSource dk = new SqlDataSource();
            dk.ConnectionString = conn;
            dk.SelectCommand = "SELECT * from pod_kategorije WHERE kat_id = " + kategorije.SelectedItem.Value;
            SUB_kategorije.DataSource = dk;
            SUB_kategorije.DataTextField = "pkategorija";
            SUB_kategorije.DataValueField = "ID";
            SUB_kategorije.DataBind();


    }
lodun