locked
data not getting or display in the dropdownlist? RRS feed

  • Question

  • User-1026236167 posted

    hello  in my code data not get or display in the dropdownlist  of related table which i provide in the query  and if data get in the dopdownlist then select item in dropdown and control goes to textbox of its related value

    cs

    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace Store
    {
    public partial class WebForm5 : 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();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
    FillGridview1();
    // FillGridview2();

    foreach (GridViewRow row in GridView2.Rows)
    {
    DropDownList ddl = (DropDownList)row.FindControl("ddl");
    // BindDropdown(ddl);
    }

    }

    //public void FillGridview2()
    //{
    // SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_products", connection);
    // DataTable DT = new DataTable();
    // adp.Fill(DT);
    // GridView2.DataSource = DT;
    // GridView2.DataBind();

    //}

    public void FillGridview1()
    {
    SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_products ", connection);
    DataTable DT = new DataTable();
    adp.Fill(DT);
    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
    try
    {
    ((DropDownList)GridView2.Rows[i].FindControl("dd3")).DataSource = DT;
    ((DropDownList)GridView2.Rows[i].FindControl("dd3")).DataBind();
    ((DropDownList)GridView2.Rows[i].FindControl("dd3")).Items.Insert(0, new ListItem() { Text = "Select", Value = "0" });
    }
    catch
    {

    }
    }
    GridView2.DataSource = DT;
    GridView2.DataBind();

    }

    protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
    {
    foreach (GridViewRow gr in GridView2.Rows)
    {
    string benefit = "0";
    TextBox tb = (TextBox)gr.FindControl("txt1");
    benefit = ((tb.Text));

    TextBox bn29 = ((TextBox)gr.FindControl("txt1"));
    bn29.Text = benefit;

    foreach (GridViewRow gr7 in GridView2.Rows)
    {
    string benefit7 = "0";
    DropDownList tb7 = (DropDownList)gr7.FindControl("dd3");
    benefit7 = ((tb7.Text));

    DropDownList bn22 = ((DropDownList)gr7.FindControl("dd3"));
    bn22.Text = benefit7;


    bn29.Text = bn22.SelectedValue.ToString();
    }
    }

    }

    }
    }

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Store.WebForm5" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" CellPadding="4" OnRowDataBound="GridView2_RowDataBound"
    ForeColor="#333333" GridLines="None" Width="186px">
    <AlternatingRowStyle BackColor="White" />
    <Columns>

    <asp:TemplateField HeaderText="main">
    <ItemTemplate>
    <asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px"
    OnSelectedIndexChanged="dd3_SelectedIndexChanged" DataTextField="category"
    DataValueField="mrp" AutoPostBack="true"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ui">
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txt1" Width="220px" Height="35px" ></asp:TextBox>
    </ItemTemplate>

    </asp:TemplateField>


    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>


    </div>
    </form>
    </body>
    </html>

    Monday, August 24, 2020 8:00 AM

Answers

  • User1535942433 posted

    Hi prabhjot1313,

    Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.You could review more and at least read some intros to EF or some video tutorials.

    Just like this:

    var context = new EntitiesContext();//DbContext Object
    var list = ent.Customers; // will return all customers.

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/ef/ef6/?redirectedfrom=MSDN

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 31, 2020 9:23 AM

All replies

  • User475983607 posted

    Typically, a DataGrid is bound the first time the page load as illustrated below.  The original code sample populates the GridView on every request which generally causes unwanted behavior because the data biding resets the GridView state and user selections

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Populate GridView
        }
    }

    The code has empty catch blocks which effectively hides exceptions while developing.  Remove the try...catch blocks until an exception handling process is established.

    The GridView is designed to edit one record at a time.  The user clicks "edit" to go from read only mode to edit mode.  This is the point where a dropdown input is populated with the selected option usually from a database record.  The user updates the record by making selections and entering data.  The user clicks save or update to save the changes in a database table and the GridView does back to read only mode.  These events are all GridView events.   I recommend learning the GridView which will help with the design. Keep in mind, the GridView has been around a long time and there are plenty of  online resources you can tap into with a basic Google search.

    Official GridView Reference.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview?view=netframework-4.8

    There might be an easier approach if you are looking to update a record when a dropdown changes.  Can you explain the design intention?

    Monday, August 24, 2020 3:05 PM
  • User1535942433 posted

    Hi prabhjot1313,

    Accroding to your description,I suggest you could use rowdatabound event bind dropdownlist.And then you could find dropdownlist and textbox in selectindexchanged event.

    More details,you could refer to below codes:

    <asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" CellPadding="4" OnDataBound="GridView2_DataBound" OnRowDataBound="GridView2_RowDataBound"
                    ForeColor="#333333" GridLines="None" Width="186px">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
    
                        <asp:TemplateField HeaderText="main">
                            <ItemTemplate>
                                <asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px" 
                                    OnSelectedIndexChanged="dd3_SelectedIndexChanged" DataTextField="employee_name"
                                    DataValueField="employee_salary" AutoPostBack="true">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ui">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="txt1" Width="220px" Height="35px"></asp:TextBox>
                            </ItemTemplate>
    
                        </asp:TemplateField>
    
    
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>

    Code-Behind:

     protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from tbl_salary";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "tbl_salary");
                this.GridView2.DataSource = ds.Tables[0].DefaultView;
                this.GridView2.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
    
            protected void GridView2_DataBound(object sender, EventArgs e)
            {    
            }
    
            protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                GridViewRow row = (GridViewRow)ddl.NamingContainer;
                TextBox txt = (TextBox)row.FindControl("txt1");
                txt.Text = ddl.SelectedItem.Value;
            }
    
            protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string str;
                    str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                    SqlConnection con = new SqlConnection(str);
                    con.Open();
                    DropDownList DropDownList1 = (e.Row.FindControl("dd3") as DropDownList);
                    SqlCommand cmd = new SqlCommand("select * from tbl_salary", con);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    con.Close();
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataBind();
                    DropDownList1.Items.Insert(0, new ListItem("--Select Category--", "0"));
                }
            }

    Result:

    Best regards,

    Yijing Sun

    Tuesday, August 25, 2020 3:19 AM
  • User-1026236167 posted

    sir i want when select some value from dropdown list then the control goes to two textboxes different fields column with onselect index changed can we add multiple data value field where control goes to two textboxes of different columns 

    Tuesday, August 25, 2020 11:57 AM
  • User475983607 posted

    sir i want when select some value from dropdown list then the control goes to two textboxes different fields column with onselect index changed can we add multiple data value field where control goes to two textboxes of different columns 

    I have no idea what you are trying to do.

    Tuesday, August 25, 2020 12:16 PM
  • User-1026236167 posted

    i want one more template coulmn when select item from dropdownliast then value filled to two textboxes as u see fill in one

    Wednesday, August 26, 2020 6:31 AM
  • User1535942433 posted

    Hi prabhjot1313,

    Accroding to your description,I'm guessing that one dropdownlist text have  multiple different values.And these values will be displayed in each textboxes.

    I suggest you could create an identity id and one column is text,another column is value in database.In your code-behind,you need to get value from database instead of dropdownlist selectedItem value.

    More details,you could refer to below codes:

     <asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" CellPadding="4" OnDataBound="GridView2_DataBound" OnRowDataBound="GridView2_RowDataBound"
                    ForeColor="#333333" GridLines="None" Width="186px">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:TemplateField HeaderText="main">
                            <ItemTemplate>
                                <asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px" 
                                    OnSelectedIndexChanged="dd3_SelectedIndexChanged" DataTextField="employee_name"
                                    DataValueField="employee_salary" AutoPostBack="true">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ui">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="txt1" Width="220px" Height="35px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ui2">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="txt2" Width="220px" Height="35px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>

    Code-Behind:

     protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from tbl_salary";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "tbl_salary");
                this.GridView2.DataSource = ds.Tables[0].DefaultView;
                this.GridView2.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
    
            }
    
            protected void GridView2_DataBound(object sender, EventArgs e)
            {
               
            }
    
            protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                GridViewRow row = (GridViewRow)ddl.NamingContainer;
                TextBox txt = (TextBox)row.FindControl("txt1");
                TextBox txt2 = (TextBox)row.FindControl("txt2");
                TextBox txt3 = (TextBox)row.FindControl("txt3");
    
    
                string str,strquery;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection con = new SqlConnection(str);
                con.Open();
                strquery = "select employee_salary from tbl_salary where employee_name='" + ddl.SelectedItem.Text + "'";
                SqlCommand cmd = new SqlCommand(strquery, con);
                SqlDataReader reader = cmd.ExecuteReader();
                List<object> chartData = new List<object>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        chartData.Add(reader["employee_salary"].ToString());
                    }
                }
                txt.Text = chartData[0].ToString();
                txt2.Text = chartData[1].ToString();
            }
            protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string str;
                    str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                    SqlConnection con = new SqlConnection(str);
                    con.Open();
                    DropDownList DropDownList1 = (e.Row.FindControl("dd3") as DropDownList);
    
    
                    SqlCommand cmd = new SqlCommand("select * from tbl_salary", con);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    con.Close();
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataBind();
                    DropDownList1.Items.Insert(0, new ListItem("--Select Category--", "0"));
    
                }
    
            }

    Result:

    Best regards,

    Yijing Sun

    Thursday, August 27, 2020 2:37 AM
  • User-1026236167 posted

    sir when i apply this code error occur index was out of range must be include non negative values

    cs

    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace Store
    {
    public partial class WebForm5 : 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();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    FillGridview1();
    }
    //FillGridview2();

    }

    public void FillGridview1()
    {
    SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_products ", connection);
    DataTable DT = new DataTable();
    adp.Fill(DT);

    GridView2.DataSource = DT;
    GridView2.DataBind();

    }

    protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
    {
    DropDownList ddl = (DropDownList)sender;
    GridViewRow row = (GridViewRow)ddl.NamingContainer;
    TextBox txt = (TextBox)row.FindControl("txt1");
    TextBox txt2 = (TextBox)row.FindControl("txt2");
    TextBox txt3 = (TextBox)row.FindControl("txt3");

    string strquery;
    String str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    con.Open();
    strquery = "select mrp from tbl_products where category='" + ddl.SelectedItem.Text + "'";
    SqlCommand cmd = new SqlCommand(strquery, con);
    SqlDataReader reader = cmd.ExecuteReader();
    List<object> chartData = new List<object>();
    if (reader.HasRows)
    {
    while (reader.Read())
    {
    chartData.Add(reader["mrp"].ToString());
    }
    }
    txt.Text = chartData[0].ToString();
    txt2.Text = chartData[1].ToString();

    }

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

    String strConnString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    con.Open();
    DropDownList DropDownList1 = (e.Row.FindControl("dd3") as DropDownList);
    SqlCommand cmd = new SqlCommand("select * from tbl_products", con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    con.Close();
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    DropDownList1.Items.Insert(0, new ListItem("--Select Category--", "0"));
    }


    }

    protected void GridView2_DataBound(object sender, EventArgs e)
    {

    }
    }
    }

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Store.WebForm5" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" CellPadding="4" OnDataBound="GridView2_DataBound" OnRowDataBound="GridView2_RowDataBound"
    ForeColor="#333333" GridLines="None" Width="186px" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>

    <asp:TemplateField HeaderText="main">
    <ItemTemplate>
    <asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px"
    OnSelectedIndexChanged="dd3_SelectedIndexChanged" DataTextField="category"
    DataValueField="mrp" AutoPostBack="true"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ui">
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txt1" Width="220px" Height="35px" ></asp:TextBox>
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="new">
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txt2" Width="220px" Height="35px" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>


    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>


    </div>
    </form>
    </body>
    </html>

    Thursday, August 27, 2020 10:51 AM
  • User1535942433 posted

    Hi prabhjot1313,

    Accroding to your description,as far as I think, if your object have one data and you have used index are big than 0.So chartData[1] is null and it will occure the error which the index is out of range.

    Since I don't know what you want to show in multiple textboxes,I create a test with one column is the  dropdownlist's id and another column is the dropdownlist's value.You could use reader.read() or datatable to read them.

    More details,you could refer to below codes:

    protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                GridViewRow row = (GridViewRow)ddl.NamingContainer;
                TextBox txt = (TextBox)row.FindControl("txt1");
                TextBox txt2 = (TextBox)row.FindControl("txt2");
    
    
                string str,strquery;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection con = new SqlConnection(str);
                con.Open();
                strquery = "select employee_salary from tbl_salary where employee_name='" + ddl.SelectedItem.Text + "'";
                SqlCommand cmd = new SqlCommand(strquery, con);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                      txt.Text = reader["Id"].ToString();
                      txt2.Text = reader["employee_salary"].ToString();
                    }
                }
            }

    Best regards,

    Yijing Sun

    Friday, August 28, 2020 4:34 AM
  • User-1026236167 posted

    sir is this code can run with entity so please execute them

    Friday, August 28, 2020 6:53 AM
  • User1535942433 posted

    Hi prabhjot1313,

    What is your entity meanings?Accroding to your codes,you follow these codes:

    <asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" CellPadding="4" OnDataBound="GridView2_DataBound" OnRowDataBound="GridView2_RowDataBound"
                    ForeColor="#333333" GridLines="None" Width="186px">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:TemplateField HeaderText="main">
                            <ItemTemplate>
                                <asp:DropDownList runat="server" ID="dd3" Width="220px" Height="35px" 
                                    OnSelectedIndexChanged="dd3_SelectedIndexChanged" DataTextField="employee_name"
                                    DataValueField="employee_salary" AutoPostBack="true">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ui">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="txt1" Width="220px" Height="35px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ui2">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="txt2" Width="220px" Height="35px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>

    Code-Behind:

     protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from tbl_salary";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "tbl_salary");
                this.GridView2.DataSource = ds.Tables[0].DefaultView;
                this.GridView2.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
    
            }
    
            protected void GridView2_DataBound(object sender, EventArgs e)
            {
               
            }
    
            protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                GridViewRow row = (GridViewRow)ddl.NamingContainer;
                TextBox txt = (TextBox)row.FindControl("txt1");
                TextBox txt2 = (TextBox)row.FindControl("txt2");
                string str, strquery;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection con = new SqlConnection(str);
                con.Open();
                strquery = "select id, employee_salary from tbl_salary where employee_name='" + ddl.SelectedItem.Text + "'";
                SqlCommand cmd = new SqlCommand(strquery, con);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        txt.Text = reader["id"].ToString();
                        txt2.Text = reader["employee_salary"].ToString();
                    }
                }
    
            }
            protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string str;
                    str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                    SqlConnection con = new SqlConnection(str);
                    con.Open();
                    DropDownList DropDownList1 = (e.Row.FindControl("dd3") as DropDownList);
    
    
                    SqlCommand cmd = new SqlCommand("select * from tbl_salary", con);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    con.Close();
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataBind();
                    DropDownList1.Items.Insert(0, new ListItem("--Select Category--", "0"));
    
                }
    
            }

    Result:

    Best regards,

    Yijing Sun

    Friday, August 28, 2020 7:50 AM
  • User-1026236167 posted

    sir i need to run this code entity framework 6.0 as  you eun with connection adapter based

    Friday, August 28, 2020 10:22 AM
  • User1535942433 posted

    Hi prabhjot1313,

    Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.You could review more and at least read some intros to EF or some video tutorials.

    Just like this:

    var context = new EntitiesContext();//DbContext Object
    var list = ent.Customers; // will return all customers.

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/ef/ef6/?redirectedfrom=MSDN

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 31, 2020 9:23 AM