其与下拉列表相关 [英] its related to dropdownlist
问题描述
我有1个下拉列表,并通过编码对其进行im绑定,并在其datatextfield属性上通过公司名称和通过公司ID通过DataValueField进行im绑定.在插入时,im使用下拉列表的选择值将公司代码插入数据库中并且它的数据类型为int,因此我将其转换为整数,但是在运行时异常,即输入字符串的格式不正确..
代码如下:=
i have 1 dropdownlist and i m binding thatby coding and on its datatextfield property i m binding by company name and DataValueField by company id.and at the time of insertion i m using the dropdown''s selected value to insert the company code in database and its datatype is int so i m converting that in integer but its not doing work giving exception at runtime that input string is not in correct format..
Code is as follows:=
try
{
SqlDataAdapter adap = new SqlDataAdapter("select br_id,br_name,cmp_id from branch where br_code='" + Session["relative_dept"].ToString() + "'", con);
DataTable dt1 = new DataTable();
adap.Fill(dt1);
if(dt1.Rows.Count > 0)
{
for(int i = 0; i < dt1.Rows.Count; i++)
{
str = dt1.Rows[0]["cmp_id"].ToString();
ddl_brid.DataSource = dt1;
ddl_brid.DataTextField = dt1.Rows[i]["br_name"].ToString();
ddl_brid.DataValueField = dt1.Rows[i]["br_id"].ToString();
ddl_brid.Items.Add(dt1.Rows[i]["br_name"].ToString());
}
}
}
catch { }
and at insertion button>
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString);
if (con.State == ConnectionState.Closed)
con.Open();
try
{
SqlCommand cmd = new SqlCommand("insert into department(cmp_id,br_id,dept_name,dept_description) values(@cmp_id,@br_id,@dept_name,@dept_description)", con);
cmd.Parameters.AddWithValue("@cmp_id", Convert.ToInt32(ddl_company.SelectedValue));
cmd.Parameters.AddWithValue("@br_id",Convert.ToInt32(ddl_brid.SelectedValue));
cmd.Parameters.AddWithValue("@dept_name",txt_deptname.Text);
cmd.Parameters.AddWithValue("@dept_description",txt_description.Text);
cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, this.GetType(), "Success", "<script>alert('Department Created');window.location.href = 'logindetails.aspx'; </script>", false);
Session["dept"] = "DA";
}
catch(Exception ex)
{
ScriptManager.RegisterStartupScript(this,this.GetType(), "Failed", "alert('Department Creation Failed.')",true);
}
推荐答案
您必须更改一些代码
Hi,
you''ve to change some code
if(dt1.Rows.Count > 0)
{
for(int i = 0; i < dt1.Rows.Count; i++)
{
ddl_brid.Items.Add(new ListItem(dt1.Rows[i]["br_name"].ToString(), dt1.Rows[i]["br_id"].ToString()));
}
}
最好的
All the Best
if(dt1.Rows.Count > 0)
{
for(int i = 0; i < dt1.Rows.Count; i++)
{
str = dt1.Rows[0]["cmp_id"].ToString();
ddl_brid.DataSource = dt1;
ddl_brid.DataTextField = dt1.Rows[i]["br_name"].ToString();
ddl_brid.DataValueField = dt1.Rows[i]["br_id"].ToString();
ddl_brid.Items.Add(dt1.Rows[i]["br_name"].ToString());
}
}
您不必一一添加.将您上面的代码块更改为
You don''t have to add it one by one. Change your above block of code to
if(dt1.Rows.Count > 0)
{
ddl_brid.DataSource = dt1;
ddl_brid.DataTextField = "br_name";
ddl_brid.DataValueField = "br_id";
ddl_brid.DataBind();
}
受保护的空白Page_Load(对象发送者,EventArgs e)
{
如果(!IsPostBack)
{
SqlConnection con;
SqlDataAdapter广告;
DataSet ds = new DataSet();
DataTable dt;
ad = new SqlDataAdapter(从JPCMstSection中选择strSectionName,intSectionId",(SqlConnection)Application.Get("con")));
ad.Fill(ds,"JPCMstSection");
dt = ds.Tables ["JPCMstSection"];
for(int i = 0; i< dt.Rows.Count; i ++)
{
dropdownlist1.Items.Add(dt.Rows [i] ["strsectionName"].ToString());
dropdownlist1.Items [i] .Value = dt.Rows [i] ["intSectionId"].ToString();
}
ds.Clear();
dt.Clear();
返回;
}
}
试试这个
问候,
Anilkumar.D
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con;
SqlDataAdapter ad;
DataSet ds = new DataSet();
DataTable dt;
ad = new SqlDataAdapter("select strSectionName,intSectionId from JPCMstSection", (SqlConnection)Application.Get("con"));
ad.Fill(ds, "JPCMstSection");
dt = ds.Tables["JPCMstSection"];
for (int i = 0; i < dt.Rows.Count; i++)
{
dropdownlist1.Items.Add(dt.Rows[i]["strsectionName"].ToString());
dropdownlist1.Items[i].Value = dt.Rows[i]["intSectionId"].ToString();
}
ds.Clear();
dt.Clear();
return;
}
}
try this
Regards,
Anilkumar.D
这篇关于其与下拉列表相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!