count max records in web service - vb.net

I want to count max records called srno from table called complaint in a database. How to write it in web service?
I am using this code
'open
Dim cmd18 As OleDbCommand
Try
'MsgBox("open")
cnn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\CRM.mdb")
cnn.Open()
msg = "select count(srno) from [Complaint] where [status]= '" & Me.RadButton6.Text & "'"
cmd18 = New OleDbCommand(msg, cnn)
str = cmd18.ExecuteScalar()
Catch ex As Exception
' MsgBox("No Records Inserted" + ex.ToString())
Finally
cnn.Close()
End Try
If str Is DBNull.Value Then
Button1.Text = "0"
Else
Button1.Text = Format(CStr(TextBox1.Text)) & Convert.ToInt32(str)
End If
i want in webservice..

You will find all your answers here http://www.asp.net/web-api

Related

Count no. of Rows on my Database table Between the selection of two Datetimepicker

How can I get the total Rows of date records on my database between the selection of two datetimepicker? I don't have any idea how to do this. Had been searching for an answer for about an hour but I cannot find the correct code. Please help me.
Here's my code:
Try
ConnDB()
If fromDate.Value > toDate.Value Then
MessageBox.Show("Invalid Date Selection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
sqlqry = "SELECT * FROM tblAttendance WHERE WorkingDate BETWEEN='" & fromDate.Value.ToShortDateString & "' AND '" & toDate.Value.ToShortDateString & "'"
da = New OleDbDataAdapter(sqlqry, cnn)
Dim dt As New DataTable("tblAttendance")
da.Fill(dt)
dgvAttendance.DataSource = dt
Dim counter As Double
counter = dt.Rows.Count
TextBox1.Text = counter
End If
Catch ex As Exception
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
End Try
It only shows 0 value if I qoute as a comment this part of the code:
da = New OleDbDataAdapter(sqlqry, cnn)
Dim dt As New DataTable("tblAttendance")
da.Fill(dt)
dgvAttendance.DataSource = dt
You must format the string expressions of the date values to fit Access SQL:
sqlqry = "SELECT * FROM tblAttendance WHERE WorkingDate BETWEEN #" & fromDate.Value.ToString("yyyy'/'MM'/dd") & "# AND #" & toDate.Value.ToString("yyyy'/'MM'/dd") & "#"
or use parameters as already suggested.
Finally figured out, Thanks for the help.
Here's my new code which really works.
Try
ConnDB()
If fromDate.Value > toDate.Value Then
MessageBox.Show("Invalid Date Selection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
sqlqry = "SELECT * FROM tblAttendance WHERE WorkingDate BETWEEN #" & fromDate.Value & "# AND #" & toDate.Value & "#"
da = New OleDbDataAdapter(sqlqry, cnn)
dt = New DataTable("tblAttendance")
da.Fill(dt)
Dim counter As Integer
counter = dt.Rows.Count
TextBox1.Text = counter
End If
Catch ex As Exception
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
End Try

VB.NET:Updating record in Ms Access

I am creating an employee timing sheet in which they have to insert their timings through pressing timein and timeout buttons. For timein I am creating a new record in database, for that person, and for timeout I am using UPDATING command to update that existing record. Here is my code:
Dim cb As New OleDb.OleDbCommandBuilder(ssda)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim str As String
str = "UPDATE emp_timing SET emp_timing.emp_timeout = '" & OnlyTime & "' WHERE (((emp_timing.emp_code)='" & TextBox1.Text & "') AND ((emp_timing.day)=" & Now.ToString("MM/dd/yyyy") & "))"
Dim cmd As OleDbCommand = New OleDbCommand(str, con)
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
MsgBox("Data added")
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
ComboBox1.SelectedIndex = -1
Catch ex As Exception
MsgBox(ex.Message)
End Try
My code is working fine but the problem is that it is not updating records in database.
Datatype for fields in Access:
emp_code = Number, emp_timeout = Text, day = Date/Time.
As usual this is caused by your code not using the Parameters collection to pass values to the database engine. This is not really understood until you hit a conversion problem as always happens with dates
str = "UPDATE emp_timing SET emp_timeout = #p1 " & _
"WHERE emp_code = #p2 AND day = #p3"
Using con = new OleDbConnection( dbProvider & dbSource)
Using cmd = New OleDbCommand(str, con)
con.Open()
cmd.Parameters.Add("#p1", OleDbType.VarWChar).Value = OnlyTime
cmd.Parameters.Add("#p2", OleDbType.Integer).Value = Convert.ToInt32(TextBox1.Text)
cmd.Parameters.Add("#p3", OleDbType.Date).Value = new DateTime(Now.Year, Now.Month, Now.Day)
Try
Dim rowsAdded = cmd.ExecuteNonQuery()
if rowsAdded > 0 Then
MsgBox("Data added")
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
ComboBox1.SelectedIndex = -1
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using

Insert multiple data into a single cell SQLyog vb.net

I am trying to develop a management system for dentists, this the system i am developing
THIS MY PROGRAM'S SCREENSHOT
when the dentist inputted a data on a textbox, it will be saved on the database and whenever the dentist insert a data again on that textbox, instead of replacing the older data with a newer data, it will store the data, making the cell store multiple data
and this is my code for adding data into the table
table name: teethhistory
database name: PatientManagementSystem
Private Sub txtURThirdMolar_KeyDown(sender As Object, e As KeyEventArgs) Handles txtURThirdMolar.KeyDown
If e.KeyCode = Keys.Enter Then
MySqlConn.Open()
query1 = "SELECT * FROM teethhistory WHERE Patient_ID_Number ='" & lblID.Text & "'"
cmd1 = New MySqlCommand(query1, MySqlConn)
reader = cmd1.ExecuteReader
If reader.HasRows Then
Dim i As Integer
With cmd
.Connection = MySqlConn
.CommandText = "UPDATE teethhistory SET Up_Right_3rd_Molar ='" & txtURThirdMolar.Text & "' WHERE Patient_ID_Number = " & lblID.Text
reader.Close()
i = .ExecuteNonQuery
End With
If i > 0 Then
MsgBox("Updated!", MsgBoxStyle.Information, "Success")
Else
MsgBox("Failed", MsgBoxStyle.Information, "Failed")
End If
Else
Dim cmd As MySqlCommand = MySqlConn.CreateCommand
cmd.CommandText = String.Format("INSERT INTO teethhistory (Patient_ID_Number, Fullname, Up_Right_3rd_Molar )" &
"VALUES ('{0}' ,'{1}' ,'{2}')",
lblID.Text,
lblFullname.Text,
txtURThirdMolar.Text)
reader.close()
Dim affectedrows As Integer = cmd.ExecuteNonQuery()
If affectedrows > 0 Then
MsgBox("Saved!", MsgBoxStyle.Information, "Success")
Else
MsgBox("Saving failed.", MsgBoxStyle.Critical, "Failed")
End If
MySqlConn.close()
End If
End Sub
if you want to Append the Existing text In field with new data from textbox,use Update command as
.CommandText = "UPDATE teethhistory SET Up_Right_3rd_Molar = concat('" & txtURThirdMolar.Text & "',Up_Right_3rd_Molar) WHERE Patient_ID_Number = " & lblID.Text
for inserting values seperated by commas, just insert a comma before the string ion concat function.
hope i undestood your problem well and this solves it.

SQL Error: "There are no rows at position 0"

I am Trying to create a login form for my software and i keep getting this feedback as error; "There are no rows at postion 0".....This happens whenever i try to add a WHERE clause to my (SLELECT ProfileName, ProfilePassword from Profile) query (i.e 'sqlQuery' in code)......This is a snippet of my code
Public Sub sql_login(ByVal sqlQuery As String)
Try
'OPENING THE CONNECTION
con.Open()
'INITIALIZE YOUR COMMANDS
'IT HOLDS THE DATA TO BE EXECUTED
With cmd
'PASS ON THE VALUE OF CON TO THE MYSQL COMMANND WHICH IS THE CONNECTION
.Connection = con
'THE FUNCTION OF THIS IS TO RETURN THE TEXT REPRESENTED BY A COMMAND OBJECT
.CommandText = sqlQuery
End With
Dim dt = New DataTable
'FOR RETRIEVING AND FILLING DATA IN THE TABLE
Dim da = New SqlDataAdapter(sqlQuery, con)
'NEW TABLE IN THE DATATABLE
da.Fill(dt)
con.Close()
'SET THE TOTAL ROWS OF THE TABLE IN A VARIABLE[MAXROWS]
Dim maxrows As Integer = dt.Rows.Count
con.Close()
'preventing non empty values
If Loginform.UsernameTextBox.Text <> "" And Loginform.PasswordTextBox.Text <> "" Then
If dt IsNot Nothing Then
'CHECKING IF THE TOTAL ROWS OF THE TABLE ARE GREATER THAN 0
If maxrows > 0 Then
'THIS WILL BE NOTIFY YOU THAT WHO WOULD BE THE USER'S
MessageBox.Show("Welcome " & dt.Rows(1).Item(0) & "!" & ControlChars.NewLine & "You have sucessfully logged in.", "Login Confirmation")
'Tracing login time
Try
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "INSERT INTO ProfileDates (ProfileName, LoginDates, ProfileID) VALUES ('" & Loginform.UsernameTextBox.Text & "',GetDate(),( SELECT TOP 1 profileID FROM Profile where ProfileName = '" & Loginform.UsernameTextBox.Text & "') )"
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Suppliers_Module.LogoutBtn.Enabled = True
Suppliers_Module.LoginButton.Enabled = False
Catch ex As Exception
con.Close()
MessageBox.Show(ex.Message)
End Try
end if
end if
end if
counting on you for your help. Thanks in advance!

How to get results from Access mdb file to Visual Basic form

I am creating sort of like a database for work. I work as an IT Technician and we have many computers that come in for reimaging. We were losing many of them so I decided to make a "database" to keep tracks of everything. These computers come in with trouble tickets and I want the user to be able to search by ticket number or by serial number. My application tells me when the field is empty, it tells me when there are no results, but I can't figure out how to make it so that if it finds a result, I want it to display all fields of that row in textboxes. If it finds more than one results, I want it to show the most recent one (the rows also include the day it was brought in). Here is what I currently have:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim cmd As OleDbCommand
If txtSearch.Text = "" Then
MsgBox("No input", MsgBoxStyle.Exclamation, "Search")
txtSearch.Focus()
ElseIf InStr(txtSearch.Text, "'") Then
MsgBox("Invalid character: '", MsgBoxStyle.Critical, "Search")
txtSearch.Text = ""
txtSearch.Focus()
ElseIf rdoTicketNumber.Checked = True Then
Dim sql = "select * from Tickets where Ticket_Number = '" & txtSearch.Text & "'"
cmd = New OleDbCommand(sql, con)
con.ConnectionString = ("provider=microsoft.jet.oledb.4.0;data source=../Database.mdb")
con.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
Try
If dr.Read = False Then
MsgBox("No Results", MsgBoxStyle.Information, "Search")
txtSearch.Text = ""
txtSearch.Focus()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
ElseIf rdoSerialNumber.Checked = True Then
Dim sql = "select * from Tickets where Asset_Serial = '" & txtSearch.Text & "'"
cmd = New OleDbCommand(sql, con)
con.ConnectionString = ("provider=microsoft.jet.oledb.4.0;data source=../Database.mdb")
con.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
Try
If dr.Read = False Then
MsgBox("No Results", MsgBoxStyle.Information, "Search")
txtSearch.Text = ""
txtSearch.Focus()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End If
End Sub
Any and all help is appreciated. Thank you in advance!
If you really just want to filter your results of the query-string:
Dim sql = "select * from Tickets where Asset_Serial = '" & txtSearch.Text & "'"
To filter it for recent one, change it to something like this:
Dim sql = "select TOP(1) * from Tickets where Asset_Serial = '" & txtSearch.Text & "' ORDER BY <yourDateField> DESC"
That you should change <yourDateField> to name of that date field in your Tickets table, and also if you have just day of ticket and you want to filter the result to most recent in that date I suggest you this:
Dim sql = "select TOP(1) * from Tickets where Asset_Serial = '" & txtSearch.Text & "' ORDER BY <yourDateField> DESC, <yourPKField> DESC"