2011-08-16 8 views
0

varcharであるデータベースの列からデータを読み込み、読み込み結果をstringに格納するようにします。変数ですが、indexoutofrange例外を生成します...基本的には、ユーザーがデータベースに既にある予約時間を入力すると、プログラムはエラーメッセージを生成するように、検証を行います。また、入力された値が予約された時間内にある場合は、エラーが生成されます。もし予約が12/12/2011午後4時にあり、期間が2時間の場合、予約は他のゲームや他の法廷の予約でない限り、プログラムは午後6時までに予約を許可しないでください選択クエリ私はdatareaderがvarcharであるデータベースの列からデータを読み込み、読み込み結果を文字列変数に格納するようにします

str2 = "select booking_date, booking_time, booking_duration, poolno, courtno, tableno from Bookings" 

    Dim a, b As Date 
    Dim c As Date 
    Dim d, f, g, l As String 

    Dim dur As Integer 

    Dim cmd2 As New SqlCommand(str2, con) 
    con.Open() 

    Dim bookchk As SqlDataReader = cmd2.ExecuteReader 
    While bookchk.Read() 
     a = bookchk("booking_date") 
     b = bookchk("booking_time") 
     c = b.ToLongTimeString 
     dur = bookchk("booking_duration") 

     l = bookchk("game") 'exception is generated here 


     If CmboGame.SelectedItem = "Swimming" Then 
      If bookchk("poolno") IsNot System.DBNull.Value Then 
       d = bookchk("poolno") 

      End If 

     Else : d = "" 

     End If 

     If CmboGame.SelectedItem = "Table Tennis" Then 
      If bookchk("tableno") IsNot System.DBNull.Value Then 
       f = bookchk("tableno") 

      End If 
     Else : f = "" 
     End If 
     If CmboGame.SelectedItem IsNot "Table Tennis" And CmboGame.SelectedItem IsNot "Swimming" Then 
      If bookchk("courtno") IsNot System.DBNull.Value Then 
       g = bookchk("courtno") 

      End If 
     Else : g = "" 
     End If 
     If TxtBookDate.Text = a And TxtBookTime.Text = c And TxtPoolNo.Text = d And TxtCrtNo.Text = g And TxtTblNo.Text = f And CmboGame.SelectedItem = l Then 
      MessageBox.Show("The date and time you have entered has already been booked" & vbCrLf & "Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop) 
      Exit Sub 

     End If 


     Dim time, h, i, j, n As DateTime 
     n = c.AddHours(dur) 
     time = TxtBookTime.Text 
     h = time.AddHours(TxtBookDur.Text) 
     i = time.ToLongTimeString 
     j = h.ToLongTimeString 


     If TxtBookDate.Text = a And TxtPoolNo.Text = d And TxtCrtNo.Text = g And TxtTblNo.Text = f And CmboGame.SelectedIndex = l And i > c And i <= n Or j > n Then 
      MessageBox.Show("The date and time you have entered has already been booked" & vbCrLf & "Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop) 

      Exit Sub 

     End If 


    End While 
    bookchk.Close() 
    con.Close() 

答えて

0

あなたのSQLを取るための文字列変数を定義する '文字列として

薄暗いSTR2:バドミントン、スカッシュ、芝生のテニスなどの場合)

私は、このために、次のコードをしようとしていますステートメント:

str2 = "select booking_date, booking_time, booking_duration, poolno, courtno, tableno from Bookings" 

「ゲーム」という列を選択していないので、明らかにデータレデイタでこれを参照することはできません。 SQL文に「ゲーム」列を追加します。存在すると仮定すると、この問題は解消されます。もちろん、他にも問題があるかもしれません。

+0

ありがとう...コード内で変更を加えたので、クエリを変更するのを忘れてしまいました。ありがとうございます:-) –

+0

重複した予約を避けるために、私はいくつかの異なるcourtno/poolno/tblnoを予約しようとした場合、または同じゲーム内で試してみると、エラーメッセージが表示されます。 –

+0

同じ質問の中で複数の質問をするのは良い考えです。その問題の詳細は何も示されていないので、誰でもアドバイスを提供することは非常に難しいです。 – SpaceBison

関連する問題