Answered by:
in database my selected dropdownlist value not saved?

Question
-
User-1026236167 posted
i have used selected index changed concept in dropdownlist employee name i have selected not saved in the database employee salary can saved in employeename employee salary is shown
my requirement is in employee name column (employee name) is shown
css
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;namespace WebApplication14
{public partial class WebForm57 : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();
public void EstablishConnection(string storeprocedure)
{
con.ConnectionString = connection;
cmd.Connection = con;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = storeprocedure;
}
public void CloseConnection()
{
cmd.Connection.Close();
cmd.Connection.Dispose();
con.Close();
}public void FillGridview()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_salary", connection);
DataTable DT = new DataTable();
adp.Fill(DT);
Gv2.DataSource = DT;
Gv2.DataBind();
}public void FillGridview1()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_salary", connection);
DataTable DT = new DataTable();
adp.Fill(DT);
dd3.DataSource = DT;
dd3.DataBind();
//dd3.Items.Insert(0, new ListItem() { Text = "Select" });}
protected void Page_Load(object sender, EventArgs e)
{
FillGridview1();
FillGridview();
}protected void Button1_Click(object sender, EventArgs e)
{EstablishConnection("Users_InsertUser1");
cmd.Parameters.Add("@employee_name", SqlDbType.VarChar, 250).Value = dd3.SelectedValue.ToString();
cmd.Parameters.Add("@employee_salary", SqlDbType.SmallMoney).Value = txt1.Text;
try { cmd.ExecuteNonQuery(); }catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }
CloseConnection();
}protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
txt1.Text = dd3.SelectedValue.ToString();
}}
}aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm57.aspx.cs" Inherits="WebApplication14.WebForm57" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">
<table>
<tr>
<td>
<span style="margin-left:40px; color:black">Select Employee</span><br /><span style="margin-left:40px; border-radius:5px;">
<asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataTextField="employee_name"
DataValueField="employee_salary" AutoPostBack="true">
</asp:DropDownList> </span><br /></td>
<td>
<span style="margin-left:40px; color:black">Employee Salary</span><br />
<span style="margin-left:40px; border-radius:5px;"><asp:TextBox runat="server" ID="txt1"
Width="220px" Height="35px" ></asp:TextBox></span><br />
</td><td><br /> <span style="margin-left:50px"><asp:Button ID="Button1" runat="server"
Text="Submit" OnClick="Button1_Click" Height="35px" Width="80px" BackColor="#3366CC" ForeColor="White"
Style="border-radius:8px"/></span> </td></tr>
</table><asp:GridView runat="server" ID="Gv2" CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="false" Width="45%" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="employee_name" HeaderText="Bill num" />
<asp:BoundField DataField="employee_salary" HeaderText="Transaction num" /></Columns>
</asp:GridView></asp:Content>
Wednesday, April 29, 2020 11:58 AM
Answers
-
User-1330468790 posted
Hi prabhjot1313,
The problem locates in the below code in yellow background.
protected void Button1_Click(object sender, EventArgs e) { EstablishConnection("Users_InsertUser1"); cmd.Parameters.Add("@employee_name", SqlDbType.VarChar, 250).Value = dd3.SelectedValue.ToString(); cmd.Parameters.Add("@employee_salary", SqlDbType.SmallMoney).Value = txt1.Text; try { cmd.ExecuteNonQuery(); } catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); } CloseConnection(); }
You set the selected text into "employee_name" column so that you will see the salary is shown in the name column when you complete the update operation.
Solution:
- Change the SelectedValue to SelectedItem.Text
- Add data rebinding for GridView and DropDownList controls when the button click event is triggered.
More details, you could refer to below code which is constructed on your codes.
.aspx Page:
<div> <table> <tr> <td> <span style="margin-left: 40px; color: black">Select Employee</span><br /> <span style="margin-left: 40px; border-radius: 5px;"> <asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataTextField="employee_name" DataValueField="employee_salary" AutoPostBack="true"> </asp:DropDownList> </span> <br /> </td> <td> <span style="margin-left: 40px; color: black">Employee Salary</span><br /> <span style="margin-left: 40px; border-radius: 5px;"> <asp:TextBox runat="server" ID="txt1" Width="220px" Height="35px"></asp:TextBox></span><br /> </td> <td> <br /> <span style="margin-left: 50px"> <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" Height="35px" Width="80px" BackColor="#3366CC" ForeColor="White" Style="border-radius: 8px" /></span> </td> </tr> </table> <asp:GridView runat="server" ID="Gv2" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" Width="45%"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="employee_name" HeaderText="Bill num" /> <asp:BoundField DataField="employee_salary" HeaderText="Transaction num" /> </Columns> </asp:GridView> </div>
Code behind: I use ConnectionStrings section, not AppSettings so you could ignore the connection string in below codes.
SqlCommand cmd = new SqlCommand(); SqlConnection con = new SqlConnection(); //I set connection strings to connection section string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; public void EstablishConnection(string storeprocedure) { con.ConnectionString = connection; cmd.Connection = con; cmd.Connection.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = storeprocedure; } public void CloseConnection() { cmd.Connection.Close(); cmd.Connection.Dispose(); con.Close(); } public void FillGridview() { SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_salary", connection); DataTable DT = new DataTable(); adp.Fill(DT); Gv2.DataSource = DT; Gv2.DataBind(); } public void FillGridview1() { SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_salary", connection); DataTable DT = new DataTable(); adp.Fill(DT); dd3.DataSource = DT; dd3.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillGridview1(); FillGridview(); } } protected void Button1_Click(object sender, EventArgs e) { EstablishConnection("Users_InsertUser1"); cmd.Parameters.Add("@employee_name", SqlDbType.VarChar, 250).Value = dd3.SelectedItem.Text; cmd.Parameters.Add("@employee_salary", SqlDbType.SmallMoney).Value = txt1.Text; try { cmd.ExecuteNonQuery(); } catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); } CloseConnection(); // Rebind data to grid view and drop down list FillGridview1(); FillGridview(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { txt1.Text = dd3.SelectedValue.ToString(); }
Demo:
Please note that you design is not very reasonable.
- If you bind the data for drop down list from tbl_salary that you will get many duplicated employee after every insert operation. Instead, you should select the employee name from another table which is used to manager employees. e.g. "tbl_employees" if yo have this table.
Hope this can help you.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 30, 2020 2:27 AM