asp.net listbox
Posted
by lodun
on Stack Overflow
See other posts from Stack Overflow
or by lodun
Published on 2010-05-07T23:00:44Z
Indexed on
2010/05/07
23:08 UTC
Read the original article
Hit count: 343
asp.net-2.0
Why this code don't work,when i want run this code vwd 2008 express show me 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(@"server=85.94.76.170\PADME; database=estudent_pioo;uid=pioo;pwd=1234567");
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");
© Stack Overflow or respective owner