You are on page 1of 3

Imports System.Data.

SqlClient
Public Class Form1
Dim con As New SqlConnection("Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\aaa.mdf;Integrated Security=True;Connect Timeout=30;User
Instance=True")
Dim ds As New DataSet
Dim da As SqlDataAdapter
Dim ptr, Count As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

con.Open()
MsgBox("Database is Successfully connected and state is " & con.State)
Dim s1, s2 As String
s1 = "SELECT DISTINCT DepCity FROM TrainTT"
da = New SqlDataAdapter(s1, con)
da.Fill(ds, "DepCities")
Count = ds.Tables("DepCities").Rows.Count
Dim i As Integer
For i = 0 To Count - 1
ComboBox1.Items.Add(ds.Tables("DepCities").Rows(i).Item(0))
Next
s2 = "SELECT DISTINCT ArrivalCity FROM TrainTT"
da = New SqlDataAdapter(s2, con)
da.Fill(ds, "ArrCities")
Count = ds.Tables("ArrCities").Rows.Count
For i = 0 To Count - 1
ComboBox2.Items.Add(ds.Tables("ArrCities").Rows(i).Item(0))
Next
con.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
con.Open()
Dim str As String
str = "Select DepTime, ArrivalTime from TrainTT where depCity='" &
ComboBox1.Text & "' and ArrivalCity='" & ComboBox2.Text & "'"
da = New SqlDataAdapter(str, con)
da.Fill(ds, "I")
con.Close()
If ComboBox1.Text = ComboBox2.Text Then
MsgBox("Incorrect Selection")
elseif ds.Tables("I").Rows.Count = 0 Then
MsgBox("No train is available")
Else
TextBox3.Visible = True
TextBox4.Visible = True
Label5.Visible = True
Label6.Visible = True
Button2.Visible = True
Label4.Visible = True
TextBox2.Visible = True
Label5.Text = "Departure from " & ComboBox1.Text
Label6.Text = "Arrival to " & ComboBox2.Text
TextBox3.Text = ds.Tables("I").Rows(0).Item(0)
TextBox4.Text = ds.Tables("I").Rows(0).Item(1)
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click

Dim s As String
Dim depdatetime As Date
Dim arrDateTime As Date
depdatetime = CDate(DateTimePicker1.Text & " " & TextBox3.Text)
arrDateTime = CDate(DateTimePicker1.Text & " " & TextBox4.Text)
s = "Insert into Reservations(CustName, DepCity, Depdatetime, ArrCity,
ArrDatetime, CustAddress, SeatClass, SeatNo) values('" & TextBox2.Text & "', '" &
ComboBox1.Text & "', '" & depdatetime & "', '" & ComboBox2.Text & "', '" &
arrDateTime & "','" & TextBox1.Text & "'," & ComboBox3.Text & ", " & ComboBox4.Text
& ")"
Dim com As New SqlCommand(s, con)
con.Open()
com.ExecuteNonQuery()
MsgBox("Successfull Reservation", MsgBoxStyle.Information, "Info")
DataGridView1.Visible = True
ds.Clear()
Dim str As String
str = "Select * from Reservations"
da = New SqlDataAdapter(str, con)
da.Fill(ds, "Res")

'DataGridView1.DataSource = ds.Tables("Res")

con.Close()
End Sub

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
If ComboBox3.SelectedIndex = 0 Then
con.Open()
Dim str As String
Dim depdatetime As Date
Dim arrDateTime As Date
depdatetime = CDate(DateTimePicker1.Text & " " & TextBox3.Text)
arrDateTime = CDate(DateTimePicker1.Text & " " & TextBox4.Text)
str = "Select ClassOneSeats.seatNo from ClassOneSeats where seatNo not
IN(select Reservations.seatno from Reservations where SeatClass=" & ComboBox3.Text
& " and Depdatetime='" & depdatetime & "')"
da = New SqlDataAdapter(str, con)
da.Fill(ds, "ClassOne")
con.Close()
Dim x As Integer
For x = 0 To ds.Tables("ClassOne").Rows.Count - 1
ComboBox4.Items.Add(ds.Tables("ClassOne").Rows(x).Item(0))
Next
DataGridView2.DataSource = ds.Tables("ClassOne")
ElseIf ComboBox3.SelectedIndex = 1 Then
MsgBox("Underconstruction")
ElseIf ComboBox3.SelectedIndex = 2 Then
MsgBox("Underconstruction")
ElseIf ComboBox3.SelectedIndex = 3 Then
MsgBox("Underconstruction")
End If
End Sub

End Class

You might also like