Thursday, 30 January 2014

Drop Down List with Text Box Operation in Asp.Net

Get data in to Drop Down List from DB and then while select particular id on Drop Down List then all related data will show in TextBoxes:-

Ans:-

Default.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>DropDownListwithTextBoxOperations</title>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
    Select EmpId:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        Name:<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
        <br />
        Sal:<asp:TextBox ID="txtSal" runat="server" ></asp:TextBox>
        <br />
        DeptId:<asp:TextBox ID="txtDeptId" runat="server" ></asp:TextBox>
        <br />
        Doj:<asp:TextBox ID="txtDoj" runat="server" ></asp:TextBox>
        <br />
        Mgrno:<asp:TextBox ID="txtMgrNo" runat="server" ></asp:TextBox>
         <br />
        ImgId:<asp:TextBox ID="txtImgId" runat="server" ></asp:TextBox>
       
     
    </div>
    </center>
    </form>
</body>
</html>

Default.aspx.cs:-

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    static SqlConnection cn;
    static SqlDataAdapter da;
    static SqlCommand cmd;
    
    static string strConn = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            BindDDLData();
        }
    }
    void BindDDLData()
    {
        cn = new SqlConnection(strConn);
        cn.Open();
        cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spGetEmpName";
        da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        DropDownList1.DataValueField = "id";
        DropDownList1.DataTextField = "id";
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "Select");
        cn.Close();
    
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        cn = new SqlConnection(strConn);
        cn.Open();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spGetEmpNameById";
        cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = DropDownList1.SelectedValue.ToString()?? "";
        da = new SqlDataAdapter(cmd);

        //DataSet ds = new DataSet();

        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            //txtName.Text=ds.Tables["Emp"].Rows[0]["name"].ToString();

            txtName.Text = dt.Rows[0]["name"].ToString();
            txtSal.Text = dt.Rows[0]["sal"].ToString();
            txtDeptId.Text = dt.Rows[0]["deptid"].ToString();
            txtDoj.Text = dt.Rows[0]["doj"].ToString();
            txtMgrNo.Text = dt.Rows[0]["mgrno"].ToString();
            txtImgId.Text = dt.Rows[0]["imgid"].ToString();
        }
        else
        {
            Response.Write("No Data in the Table..Pls Check");
        }
        
     }
}

web.config:-

<connectionStrings>
<add name="conStr" connectionString="server=192.168.0.200;database=SubsDB;user id=sa;pwd=amtpl@123"/>
</connectionStrings>

Stored Procedure:-

1.create proc spGetEmpName
as
begin
select id,name from Emp;
end
exec spGetEmpName

2.create proc spGetEmpNameById(@id int)
as
begin
select name,sal,deptid,doj,mgrno,imgid from Emp where id=@id;
end

Issue:-Too many parameter supplied.

No comments:

Post a Comment