级联下拉列表值时出错 [英] Error with cascading dropdownlist value
问题描述
嗨
我的级联下拉列表出现错误,因为下面的代码在此行旁边给出了此错误消息:
cmd.Parameters.AddWithValue(@ BizSubCateg,SubCatBizDDL.SelectedItem.Value);
,错误信息为:未设置对象引用一个对象的实例。
注意:如果用户没有选择和值,这个下拉列表应该不存储任何内容,所以它应该工作但是我不知道为什么我会遇到这个错误信息
Hi
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
(@UID,@FN,@LN,@Password,@RePass,@Email,@Country,@State,@City,@Post,@Img,@Logo,@RegDate,@Address,@UsrType,@BizCateg,@BizSubCateg)";
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;
}
else
{
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;
ad.Fill(ds);
Session["UsrNme"] = UsrNme.Text;
Response.Redirect("User panel.aspx");
}
推荐答案
检查所选项目是否合适,然后决定你要去的价值基于此插入,例如:
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);
else
cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Value);
check ..
check..
String Str=SubCatBizDDL.SelectedValue;
str获取什么值...数据然后将其转换为@BizSubCateg数据类型..
或尝试..
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);
}
else
{
cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Value);
}
这篇关于级联下拉列表值时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!