If i have two textboxes textbox1 & textbox2 and button1 in my ASP.NET and SQL Server database
Database records are:
ID Date Seats
1 15-Dec-2010 1,2
2 15-Dec-2010 3,4
3 17-Dec-2010 1,2,3,4
I want when i type 15-Dec-2010 in TextBox1 and Click on Button1 then in textbox2 The Output would be retrieve from Database using SELECT query then in TextBox2 the output would be displayed as 1,2,3,4
That the exact i want using SELECT 开发者_开发技巧query and VB.NET.
I recommend to use a Calendar-Control .You should use ASP.Net CompareValidator for validation. You should check both on client- and serverside to avoid exceptions:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1" Type="Date" Operator="DataTypeCheck" runat="server" ErrorMessage="Enter a valid Date!" EnableClientScript="true" ></asp:CompareValidator>
<asp:Button ID="BtnGetSeats" runat="server" Text="get seats" />
On the serverside you should also trigger the validation(f.e. if javascript is disabled). To answer your actual question (untested):
Private Sub BtnGetSeats_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnGetSeats.Click
Page.Validate()
If Me.IsValid Then
Dim connectionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
Dim commandText As String = "SELECT Seats FROM Table1 WHERE [Date] = @Date"
Dim allSeats As New List(Of String)
Using connection As New SqlConnection(connectionString)
Try
Dim command As New SqlCommand(commandText, connection)
command.Parameters.AddWithValue("@Date", Date.Parse(Me.Textbox1.Text))
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader
While reader.Read
Dim nextSeats As String = Convert.ToString(reader(0))
allSeats.AddRange(nextSeats.Split(","c))
End While
End Using
Catch ex As Exception
Throw 'you should log exceptions'
End Try
End Using
'list all seats comma separated in TextBox2'
TextBox2.Text = String.Join(",", allSeats.ToArray)
End If
End Sub
Use JavaScript to populate the text in the textbox
Submit the form to the server
When the result from the server returned show the result
精彩评论