Click here to Skip to main content
15,916,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SL	COURSE_CAT	          COURSECAT_ID
1	HEALTH & SAFETY	              1
2	FOOD & HYGIENE	              2
3	FIRST AID	                 3
4	FIRE SAFETY	                 4



SL	COURSE	         COURSE_ID	 COURSE_CODE
1	BB-GENTS	    1	           BBNTS
2	BB-LADIES	    1	           BBLDS
3	PIC-2 BARBER	    1	           PBRB
4	HSO-LABOUR ACC	    1	           HLBR
5	BASIC FOOD	    2	           BSFD
6	PIC-2 FOOD	    2	           PIFD
7	FIRST AID WORK	3	FIAD


What I have tried:

<asp:DropDownList ID="ddcoursecat" runat="server" Height="20px" Width="168px" OnSelectedIndexChanged="ddcoursecat_SelectedIndexChanged" AutoPostBack="True">
                                                  </asp:DropDownList>

--------------------------------------------------------------------------------------
<asp:DropDownList ID="ddcourse" runat="server" Height="20px" Width="168px" AutoPostBack="True">
                                                    </asp:DropDownList>

--------------------------------------------------------------------------------------
I have two dropdown control (Course_cat, course)
Course dd is getting populated from course table using Course_cat ID.
my intention is if a Course is selected its datavalue field should bind corresponding Course code from course_code column.
please help me to solve this.

------------------------------------------------------------------------------------


protected void ddcoursecat_SelectedIndexChanged(object sender, EventArgs e)
       {
               ddshift.Items.Insert(0, new ListItem("---SELECT---", "0"));
             //  ddcourse.Items.Insert(0, new ListItem("---SELECT---", "0"));
              // ddcourse.SelectedIndex = (0);
               try
               {
                   string CS = ConfigurationManager.ConnectionStrings["erpconnect"].ConnectionString;

                   using (SqlConnection con = new SqlConnection(CS))
                   {
                       SqlCommand cmd = new SqlCommand("SELECT  COURSE,COURSE_CODE from tbl_course WHERE COURSECAT_ID=" + ddcoursecat.SelectedValue +"", con);
                       {
                           if (ddcoursecat.SelectedIndex == 0)
                           {
                               ddcourse.Items.Insert(0, new ListItem("---SELECT---", "0"));
                          }
                           else
                           {

                               ddcourse.Items.Insert(0, new ListItem("---SELECT---", "0"));
                               con.Open();
                               ddcourse.DataSource = cmd.ExecuteReader();
                               ddcourse.DataTextField = "COURSE";
                               ddcourse.DataValueField = "COURSE_CODE";
                               ddcourse.DataBind();
                           }

                       }
                   }
               }
               catch
               {


               }

               finally
               {
                   this.Dispose();
               }
Posted
Updated 4-May-19 11:29am
Comments
Maciej Los 4-May-19 3:20am    
Please, do not repost: How can I get data from SQL table to dropdown datavalue field[^]!
I'd suggest to improve previous question and delete this one!

You did not provide enough information about your issue, so i can only recommend you to follow the instruction from this link: Fill ASP.Net Dropdown List From Database Table Using ASP.NET C#[^]
 
Share this answer
 
You are asking this over and over and ignoring questions. Why is going wrong? Your code is doing an insert. It needs to read the database and populate a list.

Why are you using ASP.NET in 2019?
 
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