Checking a database for specific dates to prevent a booking cross over - sql

I have an ASPX web form where a logged-in user will be able to choose from a number of suit jacket sizes already stored in a database table. Once chosen the jacket size along with a from and to date for a booking the data is entered into a booking table with the relevant information.
I need help with code that will ensure that when another user chooses a suit jacket size they cannot use the same dates that are stored in the booking table for that size. The output would be in a label with an error message telling the user that the jacket size is unavailable for these dates.
I am using ASP.NET and the language VB for all of this work.
Any help would be appreciated!

This can be done with an If...Then statement check and a SQL query for the lookup.
Dim mySqlConnection As New SqlConnection("Your connection string")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "SELECT [ID] FROM [Table] WHERE (JacketSize = #jacketSize AND (DateFrom <= #dateWanted AND DateTo >= #dateWanted))"
cmd.CommandType = CommandType.Text
cmd.Connection = mySqlConnection
cmd.Parameters.Add("#jacketSize", 'JacketSize field')
cmd.Parameters.Add("#dateWanted", 'DateWanted field')
mySqlConnection.Open()
reader = cmd.ExecuteReader()
If (reader.HasRows = TRUE) Then
'code to execute if date is taken'
Else
'code to execute if date is not taken'
End If
mySqlConnection.Close()
Edit: Forgot to add the parameters. Thanks Andrew Morton.

Related

Naming Column Header Based On Results From Database

net and would to have the Header Text of columns in a datagridview be named after results from the database, e.g the query in my code returns four dates,30/08/2017,04/09/2017,21/09/2017 and 03/02/2018. My aim is to have the column headers in the data grid named after those dates. Your help will highly be appreciated.
sql = "SELECT COUNT (ServiceDate) As NoOfServiceDates FROM (SELECT DISTINCT ServiceDate FROM tblattendance)"
Using command = New OleDbCommand(sql, connection)
Using reader = command.ExecuteReader
reader.Read()
ColumnNo = CInt(reader("NoOfServiceDates")).ToString
End Using
End Using
DataGridView1.ColumnCount = ColumnNo
For i = 0 To DataGridView1.Columns.Count - 1
sql = "SELECT DISTINCT ServiceDate FROM tblattendance"
Using command = New OleDbCommand(sql, connection)
Using reader = command.ExecuteReader
While reader.Read
DataGridView1.Columns(i).HeaderText = reader("ServiceDate").ToString
End While
End Using
End Using
Next
The current code re-runs the query each time through the column count loop, meaning it will set the column header for that column to all of the date values in sequence, so the last value in the query shows in the all the columns. You only need to run the query once:
Dim i As Integer = 0
sql = "SELECT DISTINCT ServiceDate FROM tblattendance"
Using command As New OleDbCommand(sql, connection), _
reader As OleDbDatareader = command.ExecuteReader()
While reader.Read
DataGridView1.Columns(i).HeaderText = reader("ServiceDate").ToString
i+= 1
End While
End Using
Additionally, this still results in two separate trips to the database, where you go once to get the count and again to get the values. Not only is this very bad for performance, it leaves you open to a bug where another user changes your data from one query to the next.
There are several ways you can get this down to one trip to the database: loading the results into memory via a List or DataTable, changing the SQL to include the count and the values together, or adding a new column each time through the list. Here's an example using the last option:
DataGridView1.Columns.Clear()
Dim sql As String = "SELECT DISTINCT ServiceDate FROM tblattendance"
Using connection As New OleDbConnection("string here"), _
command As New OleDbCommand(sql, connection)
connection.Open()
Using reader As OleDbDataReader = command.ExecuteReader()
While reader.Read
Dim column As String = reader("ServiceDate").ToString()
DataGridView1.Columns.Add(column, column)
End While
End Using
End Using
Even better if you can use something like Sql Server's PIVOT keyword in combination with the DataGridView's AutoGenerateColumns feature for DataBinding, where you will write ONE SQL statement that has both column info and data, and simply bind the result set to the grid.
The For Next is incorrect. You execute your command for every column, when you only need to execute it once. The last result from the DataReader will be the header for every column as currently written.
You should iterate through your DataReader and increment the cursor variable there:
Dim i As Integer = 0
Using command = New OleDbCommand(sql, connection)
Using reader = command.ExecuteReader
While reader.Read
DataGridView1.Columns(i).HeaderText = reader("ServiceDate").ToString
i += 1
End While
End Using
End Using

How I handle duplicated record in textbox.AutoCompleteCustomSource in VB.net

I wrote a code for auto complete textbox in vb.net.
I have textbox search for the company name in 'company_list' table.
But some company have exactly same name, so when I input the name, it shows just 1 text.
I'd like to make these two names when I input the textbox.
Below is my code.
Dim sql as String = "SELECT * FROM company_info"
cmd = New MySqlCommand(sql, con)
con.Open()
adapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable
adapter.Fill(dt)
cName.AutoCompleteCustomSource.Clear()
For Each r In dt.Rows
cName.AutoCompleteCustomSource.Add(r.item(1).ToString)
Next
con.Close()
I have two same company name as below.
[TKTech]
[TKTech]
These two are totally different company with same name, so I need to choice between them.
When I input to textbox(cName), it just show 1 name for these company.
Please let me know how I handle it.
Thank you in advance.
Frank

Filter databound combobox based on value in another column from the same dataset

I am attempting to create a process scheduler as a project in school this summer and am quite new to using VB. Currently I have a combobox bound to the "EmployeeBindingSource" which holds information for every single employee in a company. Each employee is assigned a skill level for each work station in the building. What I would like to do is if an employee is listed as having "0" skill in say, the assembly station, when selecting an employee to assign to the assembly station, that particular employee will not appear in the combobox.
From doing some research I believe that rather than binding the combobox to the data source in the designer, I will have to assign values to the each combo box in the code on a form load.
This is the access table that holds all the employee information
In the table we can see that Steve has a skill level of "0" in the AS_Level category (assembly).
However, here we can see that he still appears as an option for the assembly area when creating a new project
Currently all the data binding happens in the designer for each combo box and therefore no code has been written for the data binding. At the moment each combo box is bound to the "Full_Name" column in the access table.
Again, I am quite new to VB so I apologize if this is too vague. Let me know if I can give any more helpful information. Thank you all in advance.
After adding the code suggested here is what I have
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
datafile = "C:\Users\Jacob\Desktop\Halton\HaltonProject.accdb"
connString = provider & datafile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "SELECT * FROM [Employee] WHERE (SP_Level <> '" & 0 & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
Dim userfound As Boolean = False
Dim Full_Name As String
While dr.Read
userfound = True
Full_Name = dr("Full_Name").ToString
SP_Emp1.Items.Add(Full_Name)
SP_Emp2.Items.Add(Full_Name)
SP_Emp3.Items.Add(Full_Name)
SP_Emp4.Items.Add(Full_Name)
End While
myConnection.Close()
Works completely now!
Based on your comments and post, I assume, the checkboxes on the left enable/disable the comboboxes on that row, and since you're binding them individually if you change your query so it looks like this:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Employee] WHERE AS_Level <> 0 , myConnection) Dim dr As OleDbDataReader = cmd.ExecuteReader
Note that the where clause will vary depending on your needs, so it could be WHERE AS_Level <> 0 or WHERE SP_Level <> 0 and so on.
I have never used an Ole database so I'm not quite sure about the syntax, hope this helps.

I am trying to find out how to compare two dates in a visual basic database

I currently have a visual basic database made in Microsoft access. In this database, I allow the user to enter a date in a text box and this date gets saved to the database. I want to compare the dates "on the database!". The hard part is that the dates are entered in the same text box but saved on different rows. I need to compare the different dates in the rows of the database.
Fetch data from the database and use FOR EACH loop to compare. A sample code is as follows:
Dim con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource=Test.accdb;Persist Security Info=False;")
con.Open()
Dim cmd As New OleDbCommand("SELECT date_col FROM my_table", con)
cmd.CommandType = CommandType.Text
Dim reader As OleDbDataReader = cmd.ExecuteReader()
Dim dt As DataTable = reader.GetSchemaTable()
For Each row As DataRow In dt.Rows
If row.Item("date_col") = **your_criteria** Then
**your_other_statements**
End if
Next row
con.Close()
Hope this will help

How to get specific value in MS Access using VB.net

I'm trying to get the specific value in my Ms Access Database..
Example.
Here is my table in Ms Database..
I have 3 Columns, that is Name,Age and Telelphone number.
Here are the values ..
Row 1 = [Name = John ,Age =13,Telephone Num = 456)].
Row 2 = [Name = Mark, Age =11,telephone num = 123)].
Row 3 = [Name = Maye, Age =15,telephone num = 789)]
then Example I only want to get the Age of Mark and that is 11
or John's telephone number and that is 456..and
display it using textbox.
All I want is to get specific value and I don't care about the other value..
Well im Using Visual Studio 2012 and MS Access 2010..
heres my code..
Im new about VB.net so that im not relly familiar about the codes..^_^
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Vincelouie\Desktop\FBES INFO SYSTEM\Database1.accdb")
con.Open()
Dim selectString As String = "SELECT Age FROM Table1"
' this line passes in the SQL statement and the OleDbConnection object.
Dim cmd As OleDbCommand = New OleDbCommand(selectString, con)
'Send the CommandText to the connection, and then build an OleDbDataReader.
Dim reader As OleDbDataReader = cmd.ExecuteReader()
reader.Read()
StudentTextBox.Text = reader.GetValue(0).ToString()
'Close the reader and the related connection.
reader.Close()
con.Close()
Thank you..
All help are really appreciated..
I'm not going to teach you all about ADO.NET here. If you want to learn the basics of data access in VB.NET then there are plenty of places that you can learn that. Here are some code examples:
http://www.vbforums.com/showthread.php?469872-Retrieving-and-Saving-Data-in-Databases
Addressing this question specifically, you will create an OleDbCommand containing an appropriate SQL query and call ExecuteScalar. That thread I linked to provides an example.