i am doing onine railway reservation project .i have taken a ticket booking form where user can enter passenger detail .i have taken 6 textboxes for name,dropdownlist for age,berth,sex,class. i have written following code for ticket booking but this code is not working when after entering details this code does not calculate ticket_price for particular age.please tell me if this code is wrong then tell me right code.i am using asp.net and vb.net.i am saving all the details entered by user in database.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dr As System.Data.OleDb.OleDbDataReader
Dim insert As String
Dim connectionstring As String = "provider=MSDAORA;datasource=;user id=scott;password=tiger"
Dim oledbconnection1 As Data.OleDb.OleDbConnection = New Data.OleDb.OleDbConnection(connectionstring)
oledbconnection1.Open()
Dim mycommand As Data.OleDb.OleDbCommand
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + TextBox1.Text + "'", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()

x1 = dr.Item("reserveseats")
oledbconnection1.Close()

If (DropDownList1.SelectedValue = "ac chair class") Then
oledbconnection1.Open()
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + TextBox1.Text + "'", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()
a = dr.Item("ac_chair_class")
ElseIf (DropDownList1.SelectedValue = "executive_class") Then

oledbconnection1.Open()
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + TextBox1.Text + "'", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()
a = dr.Item("executive_class")
oledbconnection1.Close()
ElseIf (DropDownList1.SelectedValue = "first_class") Then

oledbconnection1.Open()
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + TextBox1.Text + "'", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()
a = dr.Item("first_class")
oledbconnection1.Close()
ElseIf (DropDownList1.SelectedValue = "second_class") Then
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + TextBox1.Text + "'")
dr = mycommand.ExecuteReader()
dr.Read()
a = dr.Item("second_class")
oledbconnection1.Close()
ElseIf (DropDownList1.SelectedValue = "third_class") Then
oledbconnection1.Open()
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + TextBox1.Text + "'", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()
a = dr.Item("third_class")
oledbconnection1.Close()
End If


mycommand = New Data.OleDb.OleDbCommand("select * from ritika7", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()
b = dr.Item("pnrno")
c = dr.Item("ticketno")
update = "update ritika7 set pnrno= pnrno+1 ,ticketno= ticketno+1"
mycommand = New System.Data.OleDb.OleDbCommand(update, oledbconnection1)
mycommand.ExecuteNonQuery()
oledbconnection1.Close()

If (TextBox6.Text = 1) Then
oledbconnection1.Open()
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + TextBox1.Text + "'", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()
x1 = dr.Item("reserveseats")

y = x1 + 1
mycommand = New Data.OleDb.OleDbCommand("update ritika2 set reserveseats=@textbox6.text where train_id='" + TextBox1.Text + "'")


If DropDownList2.SelectedValue = "below 12 years" Then
ticket_price = 0
ElseIf DropDownList2.SelectedValue = "above 12 years" Then
ticket_price = a
ElseIf DropDownList2.SelectedValue = "above 60 years" Then
ticket_price = 67
End If


insert = "insert into reservation2 values('" + trainno.Text + "','" + trainname.Text + "','" + source.Text + "','" + destination.Text + "','" + DropDownList1.SelectedValue + "','" + date.Text + "','" + name.Text + "','" + DropDownList2.SelectedValue + "','" + DropDownList3.SelectedValue + "','" + b + "','" + c + "','" + ticket_price + "','" + y + "')"
mycommand = New System.Data.OleDb.OleDbCommand(insert, oledbconnection1)
mycommand.ExecuteNonQuery()
oledbconnection1.Close()

ElseIf (TextBox6.Text = 2) Then
oledbconnection1.Open()
mycommand = New Data.OleDb.OleDbCommand("select * from ritika2 where train_id='" + trainid.Text + "'", oledbconnection1)
dr = mycommand.ExecuteReader()
dr.Read()
x1 = dr.Item("reserveseats")
y1 = x1 + 1
y2 = y1 + 1
mycommand = New Data.OleDb.OleDbCommand("update ritika2 set reserveseats=@textbox6.text where train_id='" + TextBox1.Text + "'")
If DropDownList2.SelectedValue = "below 12 years" Then
ticket_price = 0
ElseIf DropDownList2.SelectedValue = "above 12 years" Then
ticket_price = a
ElseIf DropDownList2.SelectedValue = "above 60 years" Then
ticket_price = 67
End If
If DropDownList4.SelectedValue = "below 12 years" Then
ticket_price1 = 0
ElseIf DropDownList4.SelectedValue = "above 12 years" Then
ticket_price1 = a
ElseIf DropDownList4.SelectedValue = "above 60 years" Then
ticket_price1 = 67
End If
insert = "insert into reservation2 values('" + Trainid.Text + "','" + Trainname.Text + "','" + source.Text + "','" + destination.Text + "','" + DropDownList1.SelectedValue + "','" + date.Text + "','" + name.Text + "','" + DropDownList2.SelectedValue + "','" + DropDownList3.SelectedValue + "','" + b + "','" + c + "','" + ticket_price + "','" + y + "')"
insert = "insert into reservation2 values('" + Trainid.Text + "','" + Trainname.Text + "','" + source.Text + "','" + destination.Text + "','" + DropDownList1.SelectedValue + "','" + date.Text + "','" + name1.Text + "','" + DropDownList4.SelectedValue + "','" + DropDownList5.SelectedValue + "','" + b + "','" + c + "','" + ticket_price + "','" + y + "')"
mycommand = New System.Data.OleDb.OleDbCommand(insert, oledbconnection1)
mycommand.ExecuteNonQuery()
oledbconnection1.Close()

End If

end sub

Dani AI

Generated

— quick diagnostic and focused fixes so the ticket_price logic actually runs and stays correct.

Your code shows a few recurring problems that commonly stop price calculation: the train row is re-queried many times (opening/closing the connection repeatedly), some OleDbCommand instances are created without a connection, string concatenation is used for SQL (risk of syntax errors and SQL injection), numeric/text conversions are not validated (TextBox6.Text vs numeric comparisons), and database updates (pnr/ticket counters + insert) are not done atomically. Verify these specific points first — missing connection on a command or ExecuteReader called when the connection is closed will throw exceptions and abort price logic before it finishes.

Checklist to fix (in order):

  • Read the train row once, cache the class-price fields, and close the reader. Avoid repeated SELECTs.
  • Use Using blocks for connection/command/reader so connections always close.
  • Use parameterized queries instead of string concatenation.
  • Validate inputs: Integer.TryParse for quantities, check rdr.HasRows and DBNull before reading columns.
  • Compute ticket_price from a numeric age value (or bind the age drop-down to a numeric price) rather than string comparisons.
  • Wrap the pnr/ticket increment + insert in a DB transaction (or use a DB sequence) to prevent race conditions.

Minimal pattern (illustrative):

Using cn As New OleDbConnection(connStr)
  cn.Open()
  Using cmd As New OleDbCommand("SELECT ac_chair_class, executive_class, first_class, second_class, third_class, reserveseats FROM ritika2 WHERE train_id = ?", cn)
    cmd.Parameters.AddWithValue("?", trainId)
    Using rdr = cmd.ExecuteReader()
      If rdr.Read() Then
        ' map DropDownList1.SelectedValue to the correct column and convert to Decimal
        ' use Integer.TryParse(TextBox6.Text, qty) before updating seats
      End If
    End Using
  End Using
End Using

Binding the class/age drop-downs to price values (as suggested) removes many conditional branches and is worth doing. — the GridView request is a separate task; keep this thread focused on fixing the booking logic.

Recommended Answers

All 3 Replies

i want a grid view control with codes

As we don't have access to yer data - what exactly is going wrong?

BTW would'nt it it easier to bind the DropDownLists to the ticketPrice - then you don't have to do your "If DropDownList2.SelectedValue = ..."

DropDownList.DataTextField="Ticket_Type"    
  DropDownList.DataValueField="Ticket_Price"

The original question was from 2008 and I doubt he is still working on that code.

Mustaffa resurrected and hijacked this old thread with his vague, unrelated question, so I'm just going to close this to avoid further confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.