Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want date value insert into table using three dropdownlist but error show

error:-Failed to convert parameter value from a ListItem to a String.

code following:-

protected void Button2_Click(object sender, EventArgs e)
{
string image = FileUpload1.FileName;

string ext = image.Substring(image.LastIndexOf("."));

if (ext.ToLower() == ".jpg" || ext.ToLower() == ".jpeg" || ext.ToLower() == ".gif")
{
FileUpload1.SaveAs(MapPath("images\\") + image);

path1 = @"images/" + image;
}


string date = DropDownList1.SelectedValue.ToString() + "-" + DropDownList2.SelectedValue.ToString() + "-" + DropDownList3.SelectedValue.ToString();


con.Open();

SqlCommand cmd = new SqlCommand("instud", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.Add("@date", SqlDbType.VarChar, 50).Value = TextBox1.Text;
cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = TextBox2.Text;
cmd.Parameters.Add("@gendr", SqlDbType.VarChar, 50).Value = RadioButtonList1.SelectedItem;
ERROR:- cmd.Parameters.Add("@dob", SqlDbType.VarChar, 50).Value =date;
cmd.Parameters.Add("@fnam", SqlDbType.VarChar, 50).Value = TextBox5.Text;
cmd.Parameters.Add("@focc", SqlDbType.VarChar, 50).Value = TextBox6.Text;
cmd.Parameters.Add("@pradd", SqlDbType.VarChar, 100).Value = TextBox7.Text;
cmd.Parameters.Add("@padd", SqlDbType.VarChar, 100).Value = TextBox8.Text;
cmd.Parameters.Add("@mob", SqlDbType.VarChar, 50).Value = TextBox9.Text;
cmd.Parameters.Add("@cid", SqlDbType.Int).Value = TextBox10.Text;
cmd.Parameters.Add("@cnam", SqlDbType.VarChar, 50).Value = TextBox11.Text;
cmd.Parameters.Add("@duration", SqlDbType.VarChar, 50).Value = TextBox12.Text;
cmd.Parameters.Add("@catgy", SqlDbType.VarChar, 50).Value = TextBox13.Text;
cmd.Parameters.Add("@img", SqlDbType.VarChar, 50).Value = path1;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
clear();
Label15.Visible = true;
Label15.Text = "Insert Record Successfully";

}
Posted

1.You do not have to convert to string the property SelectedValue of your DropDownList objects, because they are strings. This property returns the value of the selected item, and if no item is selected it returns sting.Empty.

2.The property SelectedItem of your RadioButtonList1 object returns by default null value, if no radio button from the list is selected, so the error is generated by the trying to convert null value to string.

3.The solution is to manage also this case (when the value could be null) and you have to options for doing this:
i)to set a default value of your RadioButtonList1 when the page is initialized (in your Page_Load when IsPostBack is false); This is the option that I recommend!
ii)to test in your code above, and if is null to set one valid value (like "0" or "1");
 
Share this answer
 
Convert RadioButtonList1.SelectedItem; to a valid varchar value (e.g. 0 or 1, Y or N etc) before sending it to the stored procedure.
 
Share this answer
 
Comments
Nishant.Chauhan80 9-Jul-14 1:04am    
i am understand plz explain
change this line of code

C#
cmd.Parameters.Add("@gendr", SqlDbType.VarChar, 50).Value = RadioButtonList1.SelectedItem;

to
C#
cmd.Parameters.Add("@gendr", SqlDbType.VarChar, 50).Value = RadioButtonList1.SelectedItem.Value;
 
Share this answer
 
v2

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