Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, got a problem here. As you see I created a dropdownlist where the datafield will be on the database. Codes are as follow
VB
Dim drop As String
drop = "SELECT * FROM [CourseCode]"
connection.Open()
Dim ddDr As SqlClient.SqlDataReader
Dim ddSqlCommand As New SqlClient.SqlCommand(drop, connection)
ddDr = ddSqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
drpProCode.DataSource = ddDr
drpProCode.DataTextField = "Course_Code"
drpProCode.DataValueField = "Course_Code"
drpProCode.DataBind()
connection.Close()


After that I created another table where the dropdownlist data will be added. Here is the code:
SQL
DBCmd = New SqlClient.SqlCommand("INSERT INTO tblCourses_13514(Course_Code, Course_Title, Objectives, Category, Duration ) VALUES (@Course_Code, @Course_Title, @Objectives, @Category, @Duration)", DBConn)
DBCmd.Parameters.Add("@Course_Code", SqlDbType.NVarChar).Value = drpProCode.Text
DBCmd.Parameters.Add("@Course_Title", SqlDbType.NChar).Value = txtProName.Text
DBCmd.Parameters.Add("@Objectives", SqlDbType.NVarChar).Value = txtProDescription.Text
DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = drpProCategory.Text
DBCmd.Parameters.Add("@Duration", SqlDbType.NVarChar).Value = txtProDuration.Text
DBCmd.ExecuteNonQuery()
lblProMessage.Text = "Your Proposed Plan was sent for approval"


Now my problem is when I click add, only the first record of the dropdownlist is added even when a choose a different data from it. Plss help me I dont know where the problem is. Thanks in advance and more power!
Posted
Updated 3-May-11 15:32pm
v2

Use drpProCode.SelectedValue instead of drpProCode.Text on your code.
 
Share this answer
 
Comments
janwel 3-May-11 21:26pm    
Sir the problem is still the same
put your first code that load the dropdown list in page load function and do it only once checking if its not a post back.
Because when you clik your button it do the post back on page and load the dropdown list again ignoring what you have selected. Hence it always post the first seelcted value by default.

 protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
           // load drop down here
      }
}


please change it to vb

Thanks
 
Share this answer
 
v3
Comments
janwel 3-May-11 21:34pm    
sir my codes for getting in the data for the dropdown is on Page_Load
saxenaabhi6 3-May-11 21:37pm    
are you checking if its not loading on every post back.
janwel 3-May-11 21:38pm    
sir what is the syntax for postback cause ive got an error saying it must be inside a WITH statement
saxenaabhi6 3-May-11 21:42pm    
try this:
If IsPostBack = False Then

End If
janwel 3-May-11 21:46pm    
thanks sir ive got it

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