基于下拉菜单选择保存ID数据库 [英] Saving ID to database based on dropdown selection

查看:132
本文介绍了基于下拉菜单选择保存ID数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code绑定BusinessID为ddlIndustry下拉的DataValueField。我想要做的就是所选择的ID保存到一个不同的表(公司)。我与ddlIndustry.SelectedValue这样做。由于某些原因,该第一值始终保存(1),而不是选择的值。你有什么想法,为什么这可能发生?

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{    如果(!Page.IsPostBack)
        BindCountry();
        BindIndustry();
}私人无效BindCountry()
{
    XmlDocument的DOC =新的XmlDocument();
    doc.Load(使用Server.Mappath(countries.xml));    的foreach(在doc.SelectNodes XmlNode的节点(//国))
    {
        ddlCountry.Items.Add(新的ListItem(node.InnerText,node.Attributes [code]的InnerText));
        ddlCountry.SelectedIndex = 94;
    }
}私人无效BindIndustry()
{
    CMD的SqlCommand = NULL;    康涅狄格州的SqlConnection =新的SqlConnection(GetConnectionString());
    conn.Open();    CMD =新的SqlCommand(选择行业,BusinessID FROM BusinessType,康恩);    cmd.CommandType = CommandType.Text;    SqlDataAdapter的大=新SqlDataAdapter的(CMD);    DataSet的DS =新的DataSet();    da.Fill(DS);    conn.Close();    ddlIndustry.DataSource = DS;
    ddlIndustry.DataTextField =产业;
    ddlIndustry.DataValueField =BusinessID;
    ddlIndustry.DataBind();    //ddlIndustry.Items.Insert(0,新System.Web.UI.WebControls.ListItem( - 请选择行业 - ,0));}私人无效ExecuteCompanyDetailsInsert()
{    康涅狄格州的SqlConnection =新的SqlConnection(GetConnectionString());    字符串SQL =INSERT INTO公司(BusinessID,公司名称,电子邮件,AddressLine1,AddressLine2,地点,电话)VALUES
                +(@BusinessID,@CompanyName,@Email,@ AddressLine1,@ AddressLine2,@location,@Telephone);    尝试
    {        conn.Open();
        CMD的SqlCommand =新的SqlCommand(SQL,conn);在
        的SqlParameter [] =参数新的SqlParameter [7];        参数[0] =新的SqlParameter(@ BusinessID,SqlDbType.Int);
        参数[1] =新的SqlParameter(@公司名称,SqlDbType.VarChar,50);
        参数[2] =新的SqlParameter(@电子邮件,SqlDbType.VarChar,50);
        参数[3] =新的SqlParameter(@ AddressLine1,SqlDbType.VarChar,50);
        参数[4] =新的SqlParameter(@ AddressLine2,SqlDbType.VarChar,50);
        参数[5] =新的SqlParameter(@位置,SqlDbType.VarChar,50);
        参数[6] =新的SqlParameter(@电话,SqlDbType.VarChar,50);        参数[0] .value的= ddlIndustry.SelectedValue;
        参数[1]。价值= company_name.Text;
        参数[2]。价值= company_email.Text;
        参数[3]。价值= address_line_1.Text;
        参数[4]。价值= address_line_2.Text;
        参数[5]。价值= ddlCountry.SelectedItem.Text;
        参数[6]。价值= telephone.Text;        的for(int i = 0; I< param.Length;我++)
        {
            cmd.Parameters.Add(参数[I]);
        }        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();    }    赶上(System.Data.SqlClient.SqlException前)
    {        弦乐味精=插入错误:;
        味精+ = ex.Message;
        抛出新的异常(MSG);
    }    最后
    {
        conn.Close();
    }}


解决方案

首先修改此code,因为当你的网页是回发到服务器,你的行业下拉菜单再次绑定和你的选择的值丢失

 如果(!Page.IsPostBack)
{
    BindCountry();
    BindIndustry();
}

然后你就可以得到选择的值

  ddlIndustry.SelectedValue

I am using the following code to bind BusinessID as the DataValueField of the ddlIndustry dropdown. What I want to do is save the selected ID to a different table (Company). I am doing this with ddlIndustry.SelectedValue. For some reason, the first value is always saved (1), and not the selected value. Do you have any ideas as to why this might be happening?

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
        BindCountry();
        BindIndustry();
}

private void BindCountry()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("countries.xml"));

    foreach (XmlNode node in doc.SelectNodes("//country"))
    {
        ddlCountry.Items.Add(new ListItem(node.InnerText, node.Attributes["code"].InnerText));
        ddlCountry.SelectedIndex = 94;
    }
}

private void BindIndustry()
{
    SqlCommand cmd = null;

    SqlConnection conn = new SqlConnection(GetConnectionString());
    conn.Open();

    cmd = new SqlCommand("Select Industry, BusinessID FROM BusinessType", conn);

    cmd.CommandType = CommandType.Text;

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    da.Fill(ds);

    conn.Close();

    ddlIndustry.DataSource = ds;
    ddlIndustry.DataTextField = "Industry";
    ddlIndustry.DataValueField = "BusinessID";
    ddlIndustry.DataBind();

    //ddlIndustry.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-- Please Select Industry --", "0"));

}

private void ExecuteCompanyDetailsInsert()
{

    SqlConnection conn = new SqlConnection(GetConnectionString());



    string sql = "INSERT INTO Company (BusinessID, CompanyName, Email, AddressLine1, AddressLine2, Location, Telephone) VALUES "
                + " (@BusinessID, @CompanyName, @Email, @AddressLine1, @AddressLine2, @Location, @Telephone)";

    try
    {

        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlParameter[] param = new SqlParameter[7];

        param[0] = new SqlParameter("@BusinessID", SqlDbType.Int);
        param[1] = new SqlParameter("@CompanyName", SqlDbType.VarChar, 50);
        param[2] = new SqlParameter("@Email", SqlDbType.VarChar, 50);
        param[3] = new SqlParameter("@AddressLine1", SqlDbType.VarChar, 50);
        param[4] = new SqlParameter("@AddressLine2", SqlDbType.VarChar, 50);
        param[5] = new SqlParameter("@Location", SqlDbType.VarChar, 50);
        param[6] = new SqlParameter("@Telephone", SqlDbType.VarChar, 50);

        param[0].Value = ddlIndustry.SelectedValue;
        param[1].Value = company_name.Text;
        param[2].Value = company_email.Text;
        param[3].Value = address_line_1.Text;
        param[4].Value = address_line_2.Text;
        param[5].Value = ddlCountry.SelectedItem.Text;
        param[6].Value = telephone.Text;

        for (int i = 0; i < param.Length; i++)
        {
            cmd.Parameters.Add(param[i]);
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();

    }

    catch (System.Data.SqlClient.SqlException ex)
    {

        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);
    }

    finally
    {
        conn.Close();
    }

}

解决方案

First of all modify this code, because when your page is postback to server, your industry dropdown bind again and your selected value lost

if (!Page.IsPostBack)
{
    BindCountry();
    BindIndustry();
}

and then you can get selected value

ddlIndustry.SelectedValue

这篇关于基于下拉菜单选择保存ID数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆