3
Reply

Dropdownlist error.

anil wagavkar

anil wagavkar

Mar 16 2016 3:08 AM
375
in my dropdownlist contain the name of the name product. when i select productname and insert into db i have error is.
 
 
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
 
my code is
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class visa_Master_customer_master : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=103.209.144.111;Persist Security Info=true;User ID=sparsh;Password=Visa@1234");

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//getbind();
productbind();
salsmanbind();




}

}

private void productbind()
{
SqlCommand cmd= new SqlCommand("select * from itemmaster",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource=dt;
DropDownList1.DataValueField="id";
DropDownList1.DataTextField="name";
//DropDownList1.DataValueField = "price";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("------Select Product---------", "0"));


}
private void salsmanbind()
{
SqlCommand cmd = new SqlCommand("select * from salesmans_master", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "rankcode";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("------Select Salemans---------", "0"));

}
private void getbind()
{
  SqlCommand cmd = new SqlCommand("Select id,Name,Addr1,Mobile from acmaster", con);
  SqlDataAdapter da = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  da.Fill(ds);
  GridView1.DataSource = ds;
  GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into acmaster(Name,Addr1,Mobile,Emailid,Panno,Nominee,Relation,Sponcerid,Sponcustno,Itemcode,Itemprice)values('" + customer.Text + "','" + adderss.Text + "','" + mobile.Text + "','" + emailid.Text + "','" + panno.Text + "','" + nomineen.Text + "','" + nomineer.Text + "','" + DropDownList2.SelectedItem.Value + "','" + sponcustno.Text + "','" +DropDownList1.Text+ "','" + prodid.Text + "')", con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
Label10.Text = "Data Inserted..";
con.Close();
clearfield();
}
catch (Exception ex)
{
Label10.Text = ex.Message;
}
}
private void clearfield()
{
prodid.Text = DropDownList1.Text = sponcustno.Text = nomineen.Text = nomineer.Text = panno.Text = emailid.Text =

mobile.Text = adderss.Text = customer.Text = "";
}

protected void Button2_Click(object sender, EventArgs e)
{

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
  SqlCommand cmd = new SqlCommand("delete from acmaster where id=" + id, con);
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
  getbind();
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{


prodid.Text = DropDownList1.SelectedValue.ToString();
con.Open();
SqlCommand cmd = new SqlCommand("select * from itemmaster where id=" + DropDownList1.SelectedValue);
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
prodprize.Text = dr["price"].ToString();
prodid.Text = dr["id"].ToString();
con.Close();
}
}





protected void nomineen_TextChanged(object sender, EventArgs e)
{

}
private void rank()
{
con.Open();
//getcommission();

SqlCommand cmd = new SqlCommand("select * from rank_master where id=" + DropDownList2.SelectedValue);
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
rankname.Text = dr["rank_name"].ToString();
rankid.Text = dr["id"].ToString();
//getcommission();
con.Close();
}
}






protected void commission_TextChanged(object sender, EventArgs e)
{

}
protected void Button3_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select Commission from commission_chart where Rankid='" + rankid.Text + "' and Productid='" + prodid.Text + "'");
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
commission.Text = dr["Commission"].ToString();
con.Close();
}
}
}

 

Answers (3)