locked
Dropdown RRS feed

  • Question

  • User-239141908 posted

    Can i have a reference code for 

    -binding dropdown list along with selected value for a already existing record like state etc(in page load)

    -on  update click (using stored proceduere updating the above dropdown)

    Wednesday, July 24, 2019 4:04 PM

All replies

  • User-719153870 posted

    Hi Raghava5h7,

    -binding dropdown list along with selected value for a already existing record like state etc(in page load)

    You can use ADO.NET or just a SqlDataSource control to bind data to dropdownlist.

    -on  update click (using stored proceduere updating the above dropdown)

    Use Databind() method at the end of the update envent will bind the updated value to dropdownlist again.

    Please refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True"
                     SelectCommand="select UID,UName from Users"></asp:SqlDataSource>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                     DataTextField="UName" DataValueField="UID" AutoPostBack="true" ></asp:DropDownList>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>

    CS:

    protected void Button1_Click(object sender, EventArgs e)
            {
                string constr = ConfigurationManager.ConnectionStrings["DConString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("insertuser"))
                    {
    
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@UName", TextBox1.Text);//add parameter to your stored procedure
                        cmd.Connection = con;
                        con.Open();
                        cmd.ExecuteScalar();
                        con.Close();
                    }
                }
                DropDownList1.DataBind();//bind data again
            }

    Create Users Table and stored procedure:

    create table Users
    (
    UID int identity(1,1),
    UName varchar(50)
    )
    create procedure insertuser
    @UName varchar(50)
    as
    insert into Users(UName) values(@UName)
    go

    Here's the result of this demo:

    Best Regard,

    Yang Shen

    Thursday, July 25, 2019 3:08 AM
  • User-239141908 posted

    Hi Raghava5h7,

    Raghava5h7

    -binding dropdown list along with selected value for a already existing record like state etc(in page load)

    Use Databind() method at the end of the update envent will bind the updated value to dropdownlist again.

    Please refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True"
                     SelectCommand="select UID,UName from Users"></asp:SqlDataSource>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                     DataTextField="UName" DataValueField="UID" AutoPostBack="true" ></asp:DropDownList>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>

    How can i write a function like this:   select orderID,category from [dbo].[udf_getSCategoryInfoOthers](1) in the above sql command

    1 should be taken from any textbox should be taken

    Thursday, July 25, 2019 3:03 PM
  • User-239141908 posted

    Hi Raghava5h7,

    Raghava5h7

    -binding dropdown list along with selected value for a already existing record like state etc(in page load)

    You can use ADO.NET or just a SqlDataSource control to bind data to dropdownlist.

    Raghava5h7

    -on  update click (using stored proceduere updating the above dropdown)

    Use Databind() method at the end of the update envent will bind the updated value to dropdownlist again.

    Please refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True"
                     SelectCommand="select UID,UName from Users"></asp:SqlDataSource>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                     DataTextField="UName" DataValueField="UID" AutoPostBack="true" ></asp:DropDownList>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>

    SQL Command should be like this :

    "select admissionID, medicineDetails, precautions, description, timeInDay from [dbo].[ufn_getStudentsMedicalMedicinesDetails](" + RetrievedValue + ")"

    or

    i should retrieve from a text box from previous page

    Thursday, July 25, 2019 3:08 PM
  • User-719153870 posted

    Hi Raghava5h7,

    Are there several tables in your database with the same structure but named like [Users(1)], [Users(2)]..etc?

    And you want to click the Update button to rebind the data in dropdownlist with another table?

    If so, the code will be much more simpler. And you will need no stored procedure.

    You just need to reset the SqlDataSource1.SelectCommand in the update click event.

    Please refer to below codes:

    protected void Button1_Click(object sender, EventArgs e)
            {
                SqlDataSource1.SelectCommand = "select UID,UName from [Users("+TextBox1.Text+")]";
                DropDownList1.DataBind();
            }

    In SQL, i create another table to test this demo:

    create table [Users(1)]
    (
    UID int identity(1,1),
    UName varchar(50)
    )
    
    insert into [Users(1)] values('1')
    insert into [Users(1)] values('2')
    insert into [Users(1)] values('3')
    insert into [Users(1)] values('4')
    insert into [Users(1)] values('5')

    Here's the result:

    Best Regard,

    Yang Shen

    Friday, July 26, 2019 2:51 AM