locked
How do I insert text in a textbox which will be stored in access database RRS feed

  • Question

  • Hi I am creating a booking system and I wanted to know how i can get data entered in my customer details and payment page into my database. I have used code for it. Also I had another problem I have two dropdown list boxes one is called from which lists the airports and the other is called to which should list the destinations. when I click on the from list box and select an airport thn it should display the destinations flying from that airport but its not doing tht at the moment, Please can you help me.
    Sunday, April 8, 2007 9:48 PM

Answers

  • You need to bind th second combobox to a datatable which is filled with the results of a query when the airport is selected.  You should execute the query in the AirportComboBox_SelectedValueChanged event.  For instance if you have a table the primary key of which is the name of an Airport, so that each row contains an Airport and all of its possible destinations, you would bind the first combobox to the AirportName column.  Then when the AirportName is selected you would execute a query something like this:  SELECT (Destinations) FROM AirportDestinations WHERE AirportName = @AirportName, on a duplicate table.  The SecondComboBox would be bound to the Destinations, there by always displaying the destinations of the selected Airport.   This only an example, the query is in SQL I am sorry if that is a bad assumption.  Also I assume that you only have one table for simplicity in the example, in real life it is probably more likely that you have multible tables and relations.  Anyway I hope that this helps and let me know if you need help with implementation.
    Monday, April 9, 2007 3:21 PM
  • For help with ASP.Net projects, please use the ASP forums at forums.asp.net

    How you solve your databinding issue depends on what datasource controls you're using.  In this case I would add a DataSet to the project with TableAdapters for the Airport and Destination tables.  Then I would define the relationship between the tables in the DataSet; I would also add a query to the Destination TableAdapter that loads destinations by AirPort-ID.  Next I would add an ObjectDataSource to the web form bound to the Airport TableAdapter.  This would be the datasource for the Airport dropdownlist, displaying the Airport name and holding the ID as its value.  Then I'd add another ObjectDataSource bound to the GetDestinationByAirportID query I added to the DestinationTableAdapter.  Now, in the DataSource Configuration wizard for the DestinationDropDownList you bind to the DestinationObjectDataSource and select the SelectedValue of of the AirportDropDownList as the parameter value.

    For more help, be sure to visit the ASP forums.

    Monday, April 9, 2007 6:29 PM
    Moderator
  • Well the first thing to do is to create a DataSourceComponent, again I will assume you are using SQL, DataSourceComponents are not provider independent.  Then you can use the DataSourceObject to bind the data to the text box.

     

    Hope this helps.

     

    Code Snippet

    Dim sqlETA As SqlDataSource

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

     

         sqlETA = New SqlDatasource

         sqlETA.ID = "sqlETA"

         Me.Controls.Add(sqlETA)

         sqlETA.ConnectionString = {Your Connection String}

         sqlETA.SelectCommand = "SELECT * FROM Airport Where AirportName = @AirportName"

     

         Dim AirportNameParameter As ControlParameter

         AiportNameParameter = New ControlParameter("AirportName","cbAirportName","SelectedValue")

         sqlETA.SelectParameters.Add(AirportNameParameter)

        

         sqlETA.EnableCaching = True

         sqlETA.CacheDuration = 120

     

         'Now bind the DataSource to the TextBox

         Dim dv As DataView

         dv = sqlETA.Select(DataSouceSelectArguments.Empty)

         tbETA.DataSource = dv

         tbETA.DataTextField = "ETA"

         tbETA.DataBind()

    End Sub

     

     

     

    Wednesday, April 11, 2007 10:48 PM

All replies

  •  

    Hi,

     

    Do you mean using two CombBoxes?

     

    I can't help with the dataBase code, sorry.

     

    Just add more destinations as appropriate and edit them to suit your needs.

     

    I've left the wording in CAPITAL letters.

     

     

    Regards,

     

    S_DS

     

     

    Code Snippet

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    'This example code clears the 2nd ComboBox before adding the destinations.

    ComboBox2.Items.Clear()

    'What airport was selected?

    Dim selectedAirport As String = ComboBox1.SelectedItem

    Select Case selectedAirport

    'If GATWICK these are the destinations.>>

    Case "GATWICK"

    ComboBox2.Items.Add("DUBLIN")

    ComboBox2.Items.Add("PARIS")

    'If HEATHROW these are the destinaations.>>

    Case "HEATHROW"

    ComboBox2.Items.Add("KUALA LUMPUR")

    ComboBox2.Items.Add("SYDNEY")

    'If NEWCASTLE-ENGLAND these are the destinaations.>>

    Case "NEWCASTLE-ENGLAND"

    ComboBox2.Items.Add("COPENHAGEN")

    ComboBox2.Items.Add("ROME")

    'Add more Case "Airport name here" statements here.

    'Followed by their destinations.

    End Select

    End Sub

     

     

     

     

    Monday, April 9, 2007 11:13 AM
  •  

    Hi thanks for your help but its still not working its showing me all the same destinations for each airport that I select in my dropdown lsit box which is like a combobox but in asp its called a dropdown list box. My first dropdown list box is called lstFrom which shows all the airports and the second dropdown listbox is called lstTo which should show the destinations. So if I selected for example London Heathrow (LHR) in the lstFrom dropdown listbox then it should show me all the destintions that go from London heathrow. I hope this is a better explanation to you than before.

    Monday, April 9, 2007 12:46 PM
  • You need to bind th second combobox to a datatable which is filled with the results of a query when the airport is selected.  You should execute the query in the AirportComboBox_SelectedValueChanged event.  For instance if you have a table the primary key of which is the name of an Airport, so that each row contains an Airport and all of its possible destinations, you would bind the first combobox to the AirportName column.  Then when the AirportName is selected you would execute a query something like this:  SELECT (Destinations) FROM AirportDestinations WHERE AirportName = @AirportName, on a duplicate table.  The SecondComboBox would be bound to the Destinations, there by always displaying the destinations of the selected Airport.   This only an example, the query is in SQL I am sorry if that is a bad assumption.  Also I assume that you only have one table for simplicity in the example, in real life it is probably more likely that you have multible tables and relations.  Anyway I hope that this helps and let me know if you need help with implementation.
    Monday, April 9, 2007 3:21 PM
  • For help with ASP.Net projects, please use the ASP forums at forums.asp.net

    How you solve your databinding issue depends on what datasource controls you're using.  In this case I would add a DataSet to the project with TableAdapters for the Airport and Destination tables.  Then I would define the relationship between the tables in the DataSet; I would also add a query to the Destination TableAdapter that loads destinations by AirPort-ID.  Next I would add an ObjectDataSource to the web form bound to the Airport TableAdapter.  This would be the datasource for the Airport dropdownlist, displaying the Airport name and holding the ID as its value.  Then I'd add another ObjectDataSource bound to the GetDestinationByAirportID query I added to the DestinationTableAdapter.  Now, in the DataSource Configuration wizard for the DestinationDropDownList you bind to the DestinationObjectDataSource and select the SelectedValue of of the AirportDropDownList as the parameter value.

    For more help, be sure to visit the ASP forums.

    Monday, April 9, 2007 6:29 PM
    Moderator
  •  

    Hey thanks for the help you have given me, how do i bind the dropdown list. I need some help in implementation.

    Monday, April 9, 2007 8:35 PM
  • You can bind it using the IDE or you can bind it in code.  For complex binding you need to set the DataSource and DataMember properties of the Combobox.

     

    In your case the code would probably look like this

     

    Code Snippet

    DestinationsComboBox.DataSource = AirportsDataSet.Destinations

    DestinationsComboBox.DataMember = "Destinations"

     The DataSource is the TableName and the DataMember is the ColumnName which contains the information you would like displayed in your ComboBox or ListBox.

     

    To bind a textbox you must create a binding object and add it to the DataBindings collection of the textbox

     

    Code Snippet
    ETATextBox.DataBindings.Add(New Binding("Text",AirportsDataSet.Destinations,"ETA"))

     

     

     

    Wednesday, April 11, 2007 5:26 PM
  • I highly suggest that you move to the ASP forums...  Databinding is considerably different in WebForms applications.

    Please see my previous suggestion of using the ObjectDataSource to bind to an instance of a DataTable.

    Wednesday, April 11, 2007 5:30 PM
    Moderator
  •  

    Hey thanks for the help u have given me so far you have been really helpful but one question how do i create a binding object and add it to the databindings as in web development, there isn't a toolbar for datatable or anything. Basically how do I bind a textbox. Sorry I know its alot to ask for but asp.net and all this stuff is sort of new to me.

    Wednesday, April 11, 2007 10:16 PM
  • Well the first thing to do is to create a DataSourceComponent, again I will assume you are using SQL, DataSourceComponents are not provider independent.  Then you can use the DataSourceObject to bind the data to the text box.

     

    Hope this helps.

     

    Code Snippet

    Dim sqlETA As SqlDataSource

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

     

         sqlETA = New SqlDatasource

         sqlETA.ID = "sqlETA"

         Me.Controls.Add(sqlETA)

         sqlETA.ConnectionString = {Your Connection String}

         sqlETA.SelectCommand = "SELECT * FROM Airport Where AirportName = @AirportName"

     

         Dim AirportNameParameter As ControlParameter

         AiportNameParameter = New ControlParameter("AirportName","cbAirportName","SelectedValue")

         sqlETA.SelectParameters.Add(AirportNameParameter)

        

         sqlETA.EnableCaching = True

         sqlETA.CacheDuration = 120

     

         'Now bind the DataSource to the TextBox

         Dim dv As DataView

         dv = sqlETA.Select(DataSouceSelectArguments.Empty)

         tbETA.DataSource = dv

         tbETA.DataTextField = "ETA"

         tbETA.DataBind()

    End Sub

     

     

     

    Wednesday, April 11, 2007 10:48 PM
  •  

    Hi i looked at your code and it is different to the way i have written it. The code for the destination listbox (lstTo) is shown below, it has an sql query. But it's still not working it is showing me the airport names in the Airports List box (lstFrom) and in the destination box there is nothing, and everytime i run the program it refreshes the page the airport names come up repeated. The Example below is wht comes up in the Destination (lstTo) when the page is refreshed.

     

    Bristol

    London Gatwick

    London Heathrow

    London Luton

    London Stansted

    Bristol

    London Gatwick

    London Heathrow

    London Luton

    London Stansted

     

    etc.

     

    Bristol

    London Gatwick

    London Heathrow

    London Luton

    London Stansted

     

     

    Protected Sub lstFrom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstFrom.SelectedIndexChanged

     

    'Get connection from session

    Dim objConnection As OleDbConnection = Session.Item("conn")

     

    ' SQL statement

    Dim strSQL As String = "SELECT DISTINCT Destination FROM Flight Where Origin = '" & lstFrom.SelectedItem.Text & "'"

     

    'OleDbCommand constructor

    Dim objCommand As New OleDbCommand(strSQL, objConnection)

     

    ' Extract individual fields from the OleDbDataReader

    Dim objReader As OleDbDataReader

    objReader = objCommand.ExecuteReader

    If ((objConnection.State = ConnectionState.Open) And _

    (objReader.HasRows)) Then

     

    While objReader.Read

    lstTo.Items.Add(objReader.GetValue(0))

    End While

     

    End If

    objCommand.Dispose()

    objReader.Dispose()

     

     

    Monday, April 16, 2007 4:51 PM
  •  

    While objReader.Read

    lstTo.Items.Add(objReader.GetValue(0))

    End While

     

    Well the repeating is easy enough to handle just clear the the lstTo before you add the new items

     

    Code Snippet

    While objReader.Read

       lstTo.Clear()

       lstTo.Items.Add(objReader.GetValue(0))

    End While

     

    Also are you changeing the AirportName and getting the same destinations each time?  In other words does your SQL statement function properly?  Did you enter any destinations at design time, if so remove those.

     

     

     

    Monday, April 16, 2007 5:23 PM
  • Hi I still have a problem with my list boxes. the first dropdown listbox works and it lists the airports but the second one does not show any destinations. I have a text box which is attached to a calendar. the only way the page is refreshed is if i click on the calendar button to show the calendar and then one destination shows in the destination listbox. then the same destination shows for all airports. the code for this is shown below. please can you help me. I need to hand this in on monday. Thanks.
    Friday, April 27, 2007 1:26 AM
  • This is the code to populate the first list box [origin/airport listbox]

     

    dropdown

    lstFrom.Items.Clear()

    'Get connection from session

    Dim objConnection As OleDbConnection = Session.Item("conn")

    ' SQL statement

    Dim strSQL As String = "SELECT DISTINCT Origin FROM Flight';"

    'OleDbCommand constructor

    Dim objCommand As New OleDbCommand(strSQL, objConnection)

    ' Extract individual fields from the OleDbDataReader

    Dim objReader As OleDbDataReader

    objReader = objCommand.ExecuteReader

    If ((objConnection.State = ConnectionState.Open) And _

    (objReader.HasRows)) Then

    While objReader.Read

    lstFrom.Items.Add(objReader.GetValue(0))

    End While

    End If

    objCommand.Dispose()

    objReader.Dispose()

    End Sub

     

    This is the code to populate the second listbox [destiations listbox]

    Private Sub PopulateTo()

    'Get connection from session

    Dim objConnection As OleDbConnection = Session.Item("conn")

    ' SQL statement

    Dim strSQL As String = "SELECT Destination FROM Flight WHERE Origin = ''"

    '' SQL statement

    'Dim SQL As String = "SELECT [FlightNo,DepartmentTime] FROM Flight, Date WHERE Origin = 'LHR' AND Destination = 'Athens' AND DepartureDate = '10/2/2007' AND Flight.FlightNo = Date.FlightNo"

    ' OleDbCommand(constructor)

    Dim objCommand As New OleDbCommand(strSQL, objConnection)

    ' Extract individual fields from the OleDbDataReader

    Dim objReader As OleDbDataReader

    objReader = objCommand.ExecuteReader

    If ((objConnection.State = ConnectionState.Open) And _

    (objReader.HasRows)) Then

    While objReader.Read

    lstTo.Items.Add(objReader.GetValue(0))

    End While

    End If

    objCommand.Dispose()

    objReader.Dispose()

    End Sub

     

    Protected Sub lstFrom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstFrom.SelectedIndexChanged

    ' clear dropdown listbox

    lstTo.Items.Clear()

    'Get connection from session

    Dim objConnection As OleDbConnection = Session.Item("conn")

    ' SQL statement

    Dim strSQL As String = "SELECT DISTINCT Destination FROM Flight Where Origin = '" & lstFrom.SelectedItem.Text & "'"

    'OleDbCommand constructor

    Dim objCommand As New OleDbCommand(strSQL, objConnection)

    ' Extract individual fields from the OleDbDataReader

    Dim objReader As OleDbDataReader

    objReader = objCommand.ExecuteReader

    If ((objConnection.State = ConnectionState.Open) And _

    (objReader.HasRows)) Then

    While objReader.Read

    lstTo.Items.Add(objReader.GetValue(0))

    End While

    End If

    objCommand.Dispose()

    objReader.Dispose()

    End Sub

    Friday, April 27, 2007 1:33 AM
  • Have you stepped through the code to find out why the the same destination never changes?  You need to find out if your Sql String is changing, that would be my first guess.  As for the refresh, you should be able to force the browser object to refresh, do this at the end of the lstFrom_SelectedIndexChanged event.
    Friday, April 27, 2007 1:00 PM
  • Ensure that the first DropDownList has AutoPostBack set to True.  This will cause the page to post back whenever the user changes the selected "From" airport.

    Also, as stated before, this would be MUCH easier using a DataSet and databinding.  What you are attempting to do can be done completely in the designer without writing a single line of code!

    Have you checked the ASP forums for help yet???

    Friday, April 27, 2007 2:38 PM
    Moderator
  • Hi there yes I have tried the postback on design view but it still does not process. and I have looked in asp.net forums to to help me. I also went to see my lecturer but she was also stuck.  there has to be a way to get it working. I event tried to see to debug it to see if there was something wrong but I cannot figure out what it is.

    Friday, April 27, 2007 3:42 PM