dropdownlist problem

Discussion in 'ASP.NET / ASP.NET Core' started by annbransom, Aug 27, 2006.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I have a dropdownlist that will not work. It is being populated by a datatable, which is working great. However, when I select an item from the dropdownlist and click to insert all the information on the form into my SQL database, it just inserts whatever the top value is instead of the selected value. All the other information is being inserted fine. The code is really long, so is there a particular part that might be helpful to see? Thanks!
     
  2. Often this kind of thing happens to me when you re-populate the drop-down when the page posts back. You should probably have something like this:

    if(!Page.IsPostBack)
    {
    PopulateDropDownList(); //This function will populate your drop-down
    }

    Could this be your problem?
     
  3. Nope, that didn't help. The dropdownlist is populating right - showing all the right values. The form just isn't taking the selected value. It is taking whatever the first value is in the list. I.E. If the dropdownlist populates with the following numbers:

    3
    5
    7

    And the user selects "7" and clicks submit on the form, the database gets "3".




    Imports System.Data.OleDb


    Imports System.Data


    Imports System.Data.SqlClient


    Imports system.IO


    Partial Class OhioRentalAdmin


    Inherits System.Web.UI.Page


    Dim dbConn As String = "Data Source=mssql09.discountasp.net;Initial Catalog=DB_261536_annbransom;Persist Security Info=True;User ID=;Password="


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    'must be logged in


    If Session("logger") <> True Then


    Response.Redirect("OhioIntranetEvents.aspx")


    End If


    If txtStartDate.Text <> "" And txtEndDate.Text <> "" And Page.IsPostBack Then


    PopDropDown()


    End If


    If CheckBox1.Checked Then


    DropDownList1.Enabled = False


    TextBox1.Enabled = True


    Else


    DropDownList1.Enabled = True


    TextBox1.Enabled = False


    End If


    End Sub


    Protected Sub PopDropDown()


    Dim sqlConn As SqlConnection = New SqlConnection(dbConn)


    Dim sqlCmd As SqlCommand = New System.Data.SqlClient.SqlCommand()


    Dim sqlAdapter As SqlDataAdapter = New SqlDataAdapter


    Dim dt2 As New DataTable


    Dim dt3 As New DataTable


    Dim SelectStatement As String


    Dim StartDate As Date


    Dim EndDate As Date


    Dim Location As String


    StartDate = txtStartDate.Text


    EndDate = txtEndDate.Text


    Location = RadioButtonList1.SelectedValue


    Dim SDate As Date


    SDate = StartDate


    Dim CheckDate As Date


    While SDate.Date <= EndDate.Date


    SelectStatement = "Select Ohio_Vans.VanID From Ohio_Vans, Ohio_Rentals WHERE Ohio_Vans.VanID = Ohio_Rentals.VanID AND Ohio_Rentals.Date = '" &amp; SDate &amp; " 12:00:00 AM'"


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = SelectStatement


    End With


    sqlConn.Open()


    sqlAdapter.SelectCommand = sqlCmd


    sqlAdapter.Fill(dt2)


    sqlConn.Close()


    CheckDate = DateAdd("d", 1, SDate)


    SDate = CheckDate


    End While


    If dt2.Rows.Count > 1 Then


    Dim Index As Integer


    Dim ORString As String = ""


    Dim Add As String


    For Index = 0 To dt2.Rows.Count - 2


    Add = "VanID <> '" &amp; dt2.Rows(Index).Item(0).ToString &amp; "' AND "


    ORString = ORString &amp; Add


    Next


    Dim Last As Integer


    Last = dt2.Rows.Count - 1


    Add = "VanID <> '" &amp; dt2.Rows(Last).Item(0).ToString &amp; "'"


    ORString = ORString &amp; Add


    Add = "And Location = '" &amp; Location &amp; "'"


    ORString = ORString &amp; Add


    SelectStatement = "Select * From Ohio_Vans WHERE " &amp; ORString &amp; ""


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = SelectStatement


    End With


    dt2.Clear()


    sqlConn.Open()


    sqlAdapter.SelectCommand = sqlCmd


    sqlAdapter.Fill(dt2)


    sqlConn.Close()


    DropDownList1.DataSource = dt2


    DropDownList1.DataBind()


    GridView1.DataSource = dt2


    GridView1.DataBind()


    Else


    If dt2.Rows.Count = 1 Then


    SelectStatement = "Select * From Ohio_Vans WHERE VanID <> " &amp; dt2.Rows(0).Item("VanID") &amp; " AND Location = '" &amp; Location &amp; "'"


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = SelectStatement


    End With


    dt2.Clear()


    sqlConn.Open()


    sqlAdapter.SelectCommand = sqlCmd


    sqlAdapter.Fill(dt2)


    sqlConn.Close()


    DropDownList1.DataSource = dt2


    DropDownList1.DataBind()


    GridView1.DataSource = dt2


    GridView1.DataBind()


    Else


    If dt2.Rows.Count = 0 Then


    SelectStatement = "Select * From Ohio_Vans WHERE Location = '" &amp; Location &amp; "'"


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = SelectStatement


    End With


    dt2.Clear()


    sqlConn.Open()


    sqlAdapter.SelectCommand = sqlCmd


    sqlAdapter.Fill(dt2)


    sqlConn.Close()





    DropDownList1.DataSource = dt2


    DropDownList1.DataBind()


    GridView1.DataSource = dt2


    GridView1.DataBind()


    End If


    End If


    End If





    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim sqlConn As SqlConnection = New SqlConnection(dbConn)


    Dim sqlCmd As SqlCommand = New System.Data.SqlClient.SqlCommand()


    Dim sqlAdapter As SqlDataAdapter = New SqlDataAdapter


    Dim dt As New DataTable


    Dim SelectStatement As String


    SelectStatement = "Select Top 1 RentalID From Ohio_Rentals order by RentalID desc"


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = SelectStatement


    End With


    sqlConn.Open()


    sqlAdapter.SelectCommand = sqlCmd


    sqlAdapter.Fill(dt)


    sqlConn.Close()


    Dim MassID As Integer


    MassID = dt.Rows(0).Item(0) + 1


    dt.Clear()


    Dim StartDate As Date


    Dim EndDate As Date


    Dim Location As String


    StartDate = txtStartDate.Text


    EndDate = txtEndDate.Text


    Location = RadioButtonList1.SelectedValue


    Dim CustName As String


    Dim Notes As String


    Dim DateRange As String


    Dim Ref As String


    Dim StartDay As String


    Dim StartMonth As String


    Dim StartYear As String


    Dim RentalNum As String


    Dim VanID As String


    CustName = txtTitle.Text


    CustName = Replace(CustName, "'", "''")


    Notes = txtDescription.Text


    Notes = Replace(Notes, "'", "''")





    RentalNum = txtRentalNumber.Text


    If CheckBox1.Checked Then


    VanID = TextBox1.Text


    Else


    VanID = DropDownList1.SelectedValue


    End If


    If StartDate <> EndDate Then


    DateRange = txtStartDate.Text &amp; "-" &amp; txtEndDate.Text


    Else


    DateRange = txtStartDate.Text


    End If


    Ref = DateRange &amp; "" &amp; "Rental #: " &amp; RentalNum &amp; "" &amp; "Customer: " &amp; CustName


    StartDay = StartDate.Day


    StartMonth = StartDate.Month


    StartYear = StartDate.Year





    Dim EDate As Date


    EDate = StartDate


    Dim NewDate As Date


    While EDate.Date <= EndDate.Date


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = "Insert into Ohio_Rentals (Date, CustLastName, Notes, MassID, Location, DateRange, Ref, StartDay, StartMonth, StartYear, VanID, RentalNumber) values ('" &amp; EDate &amp; "', '" &amp; CustName &amp; "', '" &amp; Notes &amp; "', '" &amp; MassID &amp; "', '" &amp; Location &amp; "', '" &amp; DateRange &amp; "', '" &amp; Ref &amp; "', '" &amp; StartDay &amp; "', '" &amp; StartMonth &amp; "', '" &amp; StartYear &amp; "', '" &amp; VanID &amp; "', '" &amp; RentalNum &amp; "')"


    .Connection.Open()


    .ExecuteNonQuery()


    .Connection.Close()


    End With


    NewDate = DateAdd("d", 1, EDate)


    EDate = NewDate


    End While


    Response.Redirect("OhioRentalAdmin.aspx")


    End Sub


    Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged


    Dim sqlConn As SqlConnection = New SqlConnection(dbConn)


    Dim sqlCmd As SqlCommand = New System.Data.SqlClient.SqlCommand()


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = "delete from Ohio_Rentals Where MassID = '" &amp; CheckBoxList1.SelectedValue &amp; "'"


    .Connection.Open()


    .ExecuteNonQuery()


    .Connection.Close()


    End With


    Response.Redirect("OhioRentalAdmin.aspx")


    End Sub


    Protected Sub LookUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LookUp.Click


    Dim sqlConn As SqlConnection = New SqlConnection(dbConn)


    Dim sqlCmd As SqlCommand = New System.Data.SqlClient.SqlCommand()


    Dim sqlAdapter As SqlDataAdapter = New SqlDataAdapter


    Dim dt4 As New DataTable


    Dim RentalNum As String


    RentalNum = lkUPRentalNUm.Text


    Dim SelectStatement As String


    SelectStatement = "Select Top 1 RentalID, Ref, MassID From Ohio_Rentals WHERE RentalNumber = '" &amp; RentalNum &amp; "' order by Ref desc"


    With sqlCmd


    .Connection = sqlConn


    .CommandType = CommandType.Text


    .CommandText = SelectStatement


    End With


    sqlConn.Open()


    sqlAdapter.SelectCommand = sqlCmd


    sqlAdapter.Fill(dt4)


    sqlConn.Close()


    CheckBoxList1.DataSource = dt4


    CheckBoxList1.DataBind()


    End Sub


    End Class
     
  4. make sure you are not populating the dropdown list after postback. in your code, it looks like might be.

    if you are populating the dropdown after postback, you can use something like this to get the value.

    xyz = Request.Form(DropDownList1.UniqueID)


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  5. That didn't help. It is still just pulling the top value off of the list instead of the selected value and sticking it in the database.
     
  6. When using "Request.Form(DropDownList1.UniqueID)", it pulls the first value in the drop down list?


    It should not do that if it is not selected.


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  7. Oh, you're right. I had that code in the wrong place. Thank you! Thank you! Thank you! I hope they pay you well!
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page