ASP.net update sql table from databound dropdown list

Walter Graulich 0 Reputation points
2023-05-30T20:36:32.4766667+00:00

I have created an aspx page that has 2 dropdown lists that are bound to the same table in sql. I use this to auto-populate the dropdown list with employee names. The dropdown list has two columns merged into one from the sql table.

The save button on the page than saves the selected item in the dropdownlists to a second separate table located in a different sql database table. When I hit the save button the name of the employee that I selected from the dropdown list does not appear in the second table. The only thing that shows up is "0" as the employee name.

Below is my cs script.

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;
using System.Configuration;

namespace OnCallWeb.Dispatch_Policies
{
    public partial class Workstation_Roles : System.Web.UI.Page
    {
         string cs = ConfigurationManager.ConnectionStrings["OnCallWebConnectionString"].ConnectionString;
         SqlConnection con;
         SqlCommand cmd;
         SqlDataAdapter adapter;
         DataTable dt;
                 
        public void DataLoad()
        {
            if (Page.IsPostBack)
            {
                dgViewRoles.DataBind();
            }
        }

        public void ClearAllData()
        {
            DropDownList1.Text = null;
            DropDownList2.Text = "";
            TextBox1.Text = "";
            DropDownList3.Text = "";
            DropDownList4.Text = null;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string mainconn = ConfigurationManager.ConnectionStrings["Reporting_SystemConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(mainconn);
            string sqlquery = "select [ID],[Emp_ID] + '  ' + [Emp_Name] as FullName from [dbo].[Personnel] where [JurisdictionID] = 99";
            SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
            sqlconn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
            sda.Fill(ds);
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "FullName";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("", "0"));
            DropDownList4.DataSource = ds;
            DropDownList4.DataTextField = "FullName";
            DropDownList4.DataValueField = "ID";
            DropDownList4.DataBind();
            DropDownList4.Items.Insert(0, new ListItem("", "0"));

            sqlconn.Close();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList1.Text = dgViewRoles.SelectedRow.Cells[2].Text;
            DropDownList2.Text = dgViewRoles.SelectedRow.Cells[3].Text;
            TextBox1.Text = dgViewRoles.SelectedRow.Cells[4].Text;
            DropDownList3.Text = dgViewRoles.SelectedRow.Cells[5].Text;
            DropDownList4.Text = dgViewRoles.SelectedRow.Cells[6].Text;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (DropDownList1.Text != "" && DropDownList2.Text != "" && TextBox1.Text != "")
            {
                using(con=new SqlConnection(cs))
                {
                    con.Open();
                    cmd = new SqlCommand("Insert Into Dispatcher_Roles (Name, Position, Roles, Status, DispatcherCovering) Values(@Name, @Position, @Roles, @Status, @DispatcherCovering)", con);
                    cmd.Parameters.AddWithValue("@Name", DropDownList1.Text);
                    cmd.Parameters.AddWithValue("@Position", DropDownList2.Text);
                    cmd.Parameters.AddWithValue("@Roles", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@Status", DropDownList3.Text);
                    cmd.Parameters.AddWithValue("@DispatcherCovering", DropDownList4.Text);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    DataLoad();
                    ClearAllData();
                }
            }
            else
            {
                lblMessage.Text = "Fill In All Information";
            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            {
                if (DropDownList1.Text != "" && DropDownList2.Text != "" && TextBox1.Text != "")
                {
                    using (con = new SqlConnection(cs))
                    {
                        con.Open();
                        cmd = new SqlCommand("Update Dispatcher_Roles Set Name=@Name, Position=@Position, Roles=@Roles, Status=@Status, DispatcherCovering=@DispatcherCovering where Name=@Name", con);
                        cmd.Parameters.AddWithValue("@Name", DropDownList1.Text);
                        cmd.Parameters.AddWithValue("@Position", DropDownList2.Text);
                        cmd.Parameters.AddWithValue("@Roles", TextBox1.Text);
                        cmd.Parameters.AddWithValue("@Status", DropDownList3.Text);
                        cmd.Parameters.AddWithValue("@DispatcherCovering", DropDownList4.Text);
                        cmd.ExecuteNonQuery();
                        con.Close();
                        DataLoad();
                        ClearAllData();
                    }
                }
                else
                {
                    lblMessage.Text = "Fill In All Information";
                }

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            using(con=new SqlConnection(cs))
            {
                con.Open();
                cmd = new SqlCommand("Delete from Dispatcher_Roles Where Name=@Name", con);
                cmd.Parameters.AddWithValue("@Name", DropDownList1.Text);
                cmd.ExecuteNonQuery();
                con.Close();
                DataLoad();
                ClearAllData();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearAllData();
        }
    }
}

Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.