Click here to Skip to main content
15,891,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the problem with my code i tried many code for this kind of error but apparently nothing's change . all i want is when i click normal(radiobutton) and select A(combobox) then in RoomNo(combobox) it will display all RoomNo that roomtype = "normal" and building = "A" .

here's all the code i tried :

VB
Try
         cn.Open()

         If rdbNormal.Checked = True Then
             Dim selectquery As String

             selectquery = " SELECT * from RoomTable Where Building='" & cmbBuilding.Text & "' and RoomType = Normal"

             Dim selectcommand As New OleDbCommand
             With selectcommand
                 .CommandText = selectquery
                 .Connection = cn
                 .ExecuteNonQuery()
             End With
             cn.Close()
         End If
     Catch ex As Exception
         MsgBox(ex.Message)
     End Try



my 2nd code :

VB
Try
           cn.Open()
           If rdbNormal.Checked = True Then
               Dim DataSet As New DataSet
               Dim DataTable As New DataTable
               Dim DataAdapter As New OleDbDataAdapter("SELECT * FROM RoomTable Where Building = '" & cmbBuilding.Text & "' And RoomType = Normal ", cn)
               DataAdapter.Fill(DataTable)

               If DataTable.Rows.Count > 0 Then
                   With cmbRoomNo
                       .Items.Clear()
                       For i As Integer = 0 To DataTable.Rows.Count - 1
                           .Items.Add(DataTable.Rows(i).Item(3))
                       Next
                       .SelectedIndex = -1
                   End With
               End If
               DataTable.Dispose()
               DataAdapter.Dispose()
           End If
       Catch ex As Exception
           MsgBox(ex.Message)
       End Try
       cn.Close()
Posted

1 solution

Firstly please do not use string concatenation in SQL statements, it exposes your database to SQL injection, as described at http://bobby-tables.com/about.html[^]. Use proper parameterised queries.
Secondly, a few seconds with your debugger would have shown you what value cmbBuilding.Text is returning.
 
Share this answer
 
Comments
Member 12011194 25-Sep-15 9:04am    
Hi sir Richard,

why i get error like "No value given one or more required parameters" is there's any wrong with my code ?
Richard MacCutchan 25-Sep-15 9:29am    
Why not try what I suggested and find out? You will be amazed how much you can learn by trying things, and getting skilled at debugging your code.
Member 12011194 25-Sep-15 9:33am    
my 1st code or my 2nd code ?
Member 12011194 25-Sep-15 9:35am    
can you give me where part should i debug my code ? need idea so that i can recode my code .
Member 12011194 25-Sep-15 10:15am    
Hi sir Richard,

i do what you said but apparently it's not working .

<pre lang="vb"> Try
If rdbNormal.Checked = True Then
Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Sherlyn\Desktop\Pernz&She5(No update)\Hospital Management System\Hospital Management System\HospitalDB.accdb"
cn = New OleDbConnection(str)
cn.Open()

Dim Selectqry As String
Selectqry = "SELECT * FROM RoomTable WHERE" & " Building = @Building AND RoomType = @RoomType "

Dim Selectcmd As New OleDbCommand
With Selectcmd
.CommandText = Selectqry
.Connection = cn
.Parameters.AddWithValue("@Building", cmbBuilding.Text)
.Parameters.AddWithValue("@RoomType", txtRT.Text)
.ExecuteNonQuery()

End With
cn.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900