Aligator3000
Aligator3000

Reputation: 345

Saving ID to database based on dropdown selection

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();
    }

}

Upvotes: 1

Views: 1466

Answers (1)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

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

Upvotes: 5

Related Questions