Click here to Skip to main content
14,920,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am facing an error with my cascading dropdownlist as the below code its gives me this error message next to this line:

cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Value);

and the error message is:" Object reference not set to an instance of an object. "

Note: this dropdown list should store nothing in case if the users didnt select and value so it should work but i dont know why i am facing this error message

protected void btnSave_Click(object sender, EventArgs e)

            HttpCookie cookie = Request.Cookies.Get("Location");
            string Location = string.Empty;
            SqlConnection cn = new SqlConnection(sc);
            SqlCommand cmd = new SqlCommand();
            Location = cookie.Value;

            string sqlstatment = @"INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,Country, State,City, Post, Img, Logo,RegDate,Address, UsrType,BizCateg,BizSubCateg) VALUES

            cmd.Connection = cn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sqlstatment;

            //Insert the parameters first
            cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
            cmd.Parameters.AddWithValue("@FN", fnbox.Text);
            cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
            cmd.Parameters.AddWithValue("@Password", passtxtbx1.Text);
            cmd.Parameters.AddWithValue("@RePass", passtxtbx2.Text);
            cmd.Parameters.AddWithValue("@Email", emailbox.Text);
            cmd.Parameters.AddWithValue("@Country", cookie.Value);
            cmd.Parameters.AddWithValue("@State", statedrdolst.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@City", citiesdrdolst.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@Post", postbox.Text);
            cmd.Parameters.AddWithValue("@Img", persimgFileUpload1.FileName);
            cmd.Parameters.AddWithValue("@Logo", logoFileUpload.FileName);
            cmd.Parameters.AddWithValue("@Address", regaddrstxtbx.Text);
            cmd.Parameters.AddWithValue("@UsrType", UsrTypeDrDo.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@BizCateg", BizCateDDL.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Value);
            //Get the Current Date Time here
            cmd.Parameters.AddWithValue("@RegDate", DateTime.Now);

            if (!string.IsNullOrEmpty(UsrNme.Text))
                Lblcheckusername.Text = "User Name Already Exist";
                Lblcheckusername.ForeColor = System.Drawing.Color.Red;
                Lblcheckusername.Text = "User Name Available";
                Lblcheckusername.ForeColor = System.Drawing.Color.Green;

            if (persimgFileUpload1.HasFile)

                persimgFileUpload1.SaveAs(Server.MapPath("~/images/users/" + persimgFileUpload1.FileName));


            if (logoFileUpload.HasFile)

                logoFileUpload.SaveAs(Server.MapPath("~/images/Logos/" + logoFileUpload.FileName));


            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.SelectCommand = cmd;
            Session["UsrNme"] = UsrNme.Text;
            Response.Redirect("User panel.aspx");


String Str=SubCatBizDDL.SelectedValue;

what value str getting..ifhas data then convert it to @BizSubCateg datatype..

or try..
if(BizCateDDL.SelectedItem.Value!="" && SubCatBizDDL.SelectedValu!="")
   cmd.Parameters.AddWithValue("@BizSubCateg", DBNull.Value);
  cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Value);
check the item selected or not and then decide the value you going to insert based on that, for example:
if(SubCatBizDDL.SelectedIndex < 0)
	cmd.Parameters.AddWithValue("@BizSubCateg", DBNull.Value);
	cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Value);
Member 10690878 6-Dec-14 20:48pm
Hi @DamithSL i have did as you mention above and its working fine, and i have tried to do it with BizCateDDL but it inserting "Your Business Field" as its writting in postback of BizCatDDL: if (!IsPostBack)

DataTable BizCateDT = new DataTable();
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
// Create the SelectCommand.
SqlCommand BizCateCmd = new SqlCommand("SELECT BizCate FROM BizCate", con2);

SqlDataAdapter adaptar = new SqlDataAdapter();
adaptar.SelectCommand = BizCateCmd;

BizCateDDL.DataSource = BizCateDT;
BizCateDDL.DataTextField = "BizCate";


BizCateDDL.Items.Insert(0, new ListItem("Your Business Field", ""));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900