Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Messaging;
using System.Net;
using System.Net.Mail;


namespace exam
{
    public partial class CreatAgroup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["seniorConnectionString"].ConnectionString);
            con.Open();
            using (SqlCommand cmd = new SqlCommand("select st_name,STID from dbo.[Student] ", con))
            {

                using (SqlDataReader read = cmd.ExecuteReader())
                {




                    DropDownList3.DataSource =  read ;


                    DropDownList3.DataTextField = "st_name";
                    DropDownList3.DataValueField = "STID";

                    DropDownList3.DataBind();
                }
            }
            using (SqlCommand cmd = new SqlCommand("select S_name,SID from dbo.[Suporvisor(commtiee)] ", con))
            {

                using (SqlDataReader read = cmd.ExecuteReader())
                {




                    DropDownList2.DataSource = read;


                    DropDownList2.DataTextField = "S_name";
                    DropDownList2.DataValueField = "SID";

                    DropDownList2.DataBind();
                }
            }
           
            

        }
        protected void DropDownList2_SelectedIndexChanged(Object Sender, EventArgs e)
        {
            Label1.Text = DropDownList2.SelectedValue.ToString();
            
        }
        protected void DropDownList3_SelectedIndexChanged(Object Sender, EventArgs e)
        {
            Label2.Text = DropDownList3.SelectedValue.ToString();

             
        }
          
    }
} if i want for example select second name in drowpdown list it is just allow select the first one

And this source code
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreatAgroup.aspx.cs" Inherits="exam.CreatAgroup" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            text-align: center;
            font-size: xx-large;
        }
        .auto-style2 {
            width: 100%;
        }
        .auto-style3 {
            width: 486px;
        }
        .auto-style4 {
            margin-left: 932px;
            margin-top: 69px;
        }
        .auto-style5 {
            width: 278px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <p class="auto-style1">
            Add Group for Sineor Project</p>
        <p class="auto-style1">
             </p>
        <table class="auto-style2">
            <tr>
                <td class="auto-style3">Student</td>
                <td>Suporvisor</td>
            </tr>
            <tr>
                <td class="auto-style3">
                    <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server"  AutoPostBack="true" Height="42px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" Width="255px">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
        <p>
             </p>
        <table class="auto-style2">
            <tr>
                <td class="auto-style5">Selected Student name</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style5">Selected Suporvisor name</td>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
        <p>
             </p>
        <p>
             </p>
        <p>
             </p>
        <p>
             </p>
        <p>
             </p>
        <p>
             Create a Groub   <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </p>
        <asp:Button ID="Button1" runat="server" CssClass="auto-style4" Height="52px" Text="Add Groub" Width="347px" />
    </form>
</body>
</html>


What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Messaging;
using System.Net;
using System.Net.Mail;


namespace exam
{
    public partial class CreatAgroup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["seniorConnectionString"].ConnectionString);
            con.Open();
            using (SqlCommand cmd = new SqlCommand("select st_name,STID from dbo.[Student] ", con))
            {

                using (SqlDataReader read = cmd.ExecuteReader())
                {




                    DropDownList3.DataSource =  read ;


                    DropDownList3.DataTextField = "st_name";
                    DropDownList3.DataValueField = "STID";

                    DropDownList3.DataBind();
                }
            }
            using (SqlCommand cmd = new SqlCommand("select S_name,SID from dbo.[Suporvisor(commtiee)] ", con))
            {

                using (SqlDataReader read = cmd.ExecuteReader())
                {




                    DropDownList2.DataSource = read;


                    DropDownList2.DataTextField = "S_name";
                    DropDownList2.DataValueField = "SID";

                    DropDownList2.DataBind();
                }
            }
           
            

        }
        protected void DropDownList2_SelectedIndexChanged(Object Sender, EventArgs e)
        {
            Label1.Text = DropDownList2.SelectedValue.ToString();
            
        }
        protected void DropDownList3_SelectedIndexChanged(Object Sender, EventArgs e)
        {
            Label2.Text = DropDownList3.SelectedValue.ToString();

             
        }
          
    }
}
Posted
Updated 3-Mar-20 1:47am
v4
Comments
phil.o 2-Mar-20 3:36am    
Unclear. What behaviour are you expecting? Which one do you experience instead?
Please improve your question.
Stefan_Lang 2-Mar-20 7:59am    
This looks like C#, not C++. Please set the correct tags on your question.

Also you mixed code, Your explanatory text, and some HTML source into the code formatting. Please separate these and use the appropriate formatting for each.

Finally, when you post so much code, you should put in some effort to point out where in the code it is not working as expected. That is very little work for you, but takes a lot of effort for anyone trying to help.

1 solution

You are re-binding both lists on every page load. You need to wrap that code in an if (!IsPostBack) test.

Also, wrap your SqlConnection in a using block.

And the SelectedValue already returns a string; there's no need to call .ToString() on it.
C#
public partial class CreatAgroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["seniorConnectionString"].ConnectionString))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand("select st_name, STID from dbo.[Student]", con))
                using (SqlDataReader read = cmd.ExecuteReader())
                {
                    DropDownList3.DataSource =  read;
                    DropDownList3.DataTextField = "st_name";
                    DropDownList3.DataValueField = "STID";
                    DropDownList3.DataBind();
                }
                
                using (SqlCommand cmd = new SqlCommand("select S_name, SID from dbo.[Suporvisor(commtiee)]", con))
                using (SqlDataReader read = cmd.ExecuteReader())
                {
                    DropDownList2.DataSource = read;
                    DropDownList2.DataTextField = "S_name";
                    DropDownList2.DataValueField = "SID";
                    DropDownList2.DataBind();
                }
            }
        }
    }
    
    protected void DropDownList2_SelectedIndexChanged(Object Sender, EventArgs e)
    {
        Label1.Text = DropDownList2.SelectedValue;
    }
    
    protected void DropDownList3_SelectedIndexChanged(Object Sender, EventArgs e)
    {
        Label2.Text = DropDownList3.SelectedValue;
    }
}

NB: You should avoid using "special" characters in your SQL table and column names. Stick to using A-Z, 0-9, and the underscore character _. As you've discovered, using "special" characters makes your queries harder to write, because you have to enclose the name in square brackets.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900