0

Why doesn't this code work? When I run it VWD Express shows this error:

Object reference not set to an instance of an object.

Line 73: kom.Parameters.Add("@subcategories", SqlDbType.Text).Value = s_categoreis.SelectedItem.ToString();

This is my ascx file:

 <asp:ListBox ID="categories" runat="server" Height="380px" CssClass="kat" AutoPostBack="true" DataSourceID="SqlDataSource1" 
        DataTextField="Categories" DataValueField="ID" 
        onselectedindexchanged="kategorije_SelectedIndexChanged"></asp:ListBox>
    
    
     <asp:Button ID="Button1" CssClass="my" runat="server" Text="click" 
            onclick="Button1_Click" />
        
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate> <asp:ListBox ID="s_categoreis" CssClass="pod" Height="150px" Enabled="true" runat="server"></asp:ListBox></ContentTemplate>
           <Triggers>
           <asp:AsyncPostBackTrigger ControlID="categories" EventName="SelectedIndexChanged" />
           
           </Triggers>
        </asp:UpdatePanel>
    
     
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:estudent_piooConnectionString %>" 
        SelectCommand="SELECT [ID], [Categories] FROM [categories]">
    </asp:SqlDataSource>

and this is my ascx.cs:

 SqlConnection veza;
    SqlCommand kom = new SqlCommand();
    SqlParameter par1 = new SqlParameter();
    SqlParameter par2 = new SqlParameter();
    SqlParameter par3 = new SqlParameter();
    SqlParameter par4 = new SqlParameter();
    SqlParameter par5 = new SqlParameter();
    SqlParameter par6 = new SqlParameter();
    SqlParameter par7 = new SqlParameter();
    SqlParameter par8 = new SqlParameter();
    SqlParameter par9 = new SqlParameter();

    protected void Page_Load(object sender, EventArgs e)
    {                          
            Listapod_kategorije(1);            
    }

    protected void kategorije_SelectedIndexChanged(object sender, EventArgs e)
    {
        Listapod_kategorije(Convert.ToInt32(kategorije.SelectedValue));    
    }

    private void Listapod_kategorije(int broj) {

        SqlDataSource ds = new SqlDataSource();
        ds.ConnectionString = ConfigurationManager.ConnectionStrings["estudent_piooConnectionString"].ConnectionString;
        ds.SelectCommand = "Select * from pod_kategorije where kat_id=" + broj;
        pod_kategorije.DataSource = ds;
        pod_kategorije.DataTextField = "pkategorija";
        pod_kategorije.DataValueField = "ID";
        pod_kategorije.DataBind();                
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Guid jk = new Guid();

        object datum = DateTime.Now;
        veza = new SqlConnection(@"...");
      
            Random broj = new Random();
            int b_kor = broj.Next(1, 1000);
            kom.Parameters.Add("@text", SqlDbType.Text).Value = str;
            kom.Parameters.Add("@user", SqlDbType.UniqueIdentifier).Value = jk;

            kom.Parameters.Add("@date", SqlDbType.DateTime).Value = datum;
            kom.Parameters.Add("@visits", SqlDbType.Int).Value = 0;
            kom.Parameters.Add("@answers", SqlDbType.Int).Value = 0;
            kom.Parameters.Add("@username", SqlDbType.Text).Value = "unknown_" + b_kor.ToString(); ;
            kom.Parameters.Add("@categories", SqlDbType.Text).Value = categories.SelectedItem.ToString();
            kom.Parameters.Add("@sub_categories", SqlDbType.Text).Value = s_categoreis.SelectedItem.ToString();
            veza.Open();
            kom.ExecuteNonQuery();
            veza.Close();
            Response.Redirect("default.aspx");

4 Answers 4

2

I can't tell where s_categoreis is being bound from your code. My guess is that you are binding the DataSource of your list box on post back, which would cause any selection you made to be lost after your button click.

Make sure you check if the page has posted back before binding your List Box, like so:

if (!IsPostBack)
{
  //bind s_categoreis here
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's being bound by the DataSourceId="SqlDataSource1" (catchy name, eh?) and associated fields. It's difficult to know what's going on here, because the code looks half translated. The listbox is s_categoreis but one of the methods is kategorije_SelectedIndexChanged . I had assumed that we weren't looking at the exact code, but you may be right, there probably is more that we aren't seeing.
0

I'd guess that the s_categoreis.SelectedItem property is null. This would be caused by the list box not having a selected value.

You can test for some of this by using the debugger and stepping through the code, hovering the mouse over identifiers, and using the watch / quick watch / immediate window.

1 Comment

Yes,s_categoreis.SelectedItem property is null, but why?I choose item from s_categoreis control..
0

SelectedItem is a ListItem object, you should change

s_categoreis.SelectedItem.ToString();

to either:

s_categoreis.SelectedItem.Text;

or:

s_categoreis.SelectedItem.Value;

1 Comment

While that is true, it's not going to help him since SelectedItem is null.
0
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:estudent_piooConnectionString %>" 
    SelectCommand="SELECT [ID], [Categories] FROM [categories]">
</asp:SqlDataSource>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.