What is wrong with my vb.net webmethod? - sql

This webmethod retrieves plist, firstaname, lastname, orgid from database then insert 2 lines in 2 different tables. 1st sql is fine - 2nd doesnt run
<WebMethod()> _
Public Function Register(ByVal meetingid As String, ByVal myid As String, ByVal PartType As Integer, ByVal startDate As String) As String
Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("EPBconnection").ConnectionString)
Dim sSQL As String
Dim plistid As String = ""
Dim LastName As String = ""
Dim FirstName As String = ""
Dim orgID As String = ""
'get plistID
sSQL = "select pl.PLIST_ID"
sSQL = sSQL + " From PERSON_LIST pl"
sSQL = sSQL + " Where MTG_ID = '" + meetingid + "'"
Dim myCommand As New SqlCommand(sSQL, connection)
connection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader
While myReader.Read()
plistid = myReader("PLIST_ID").ToString()
End While
connection.Close()
'get firstname, lastname, orgid
sSQL = "SELECT p.LASTNAME, p.FIRSTNAME, p.ORGA_ID FROM PERSON p WHERE PERSON_ID = '" + myid + "'"
myCommand = New SqlCommand(sSQL, connection)
connection.Open()
myReader = myCommand.ExecuteReader
While myReader.Read()
LastName = myReader("LASTNAME").ToString()
FirstName = myReader("FIRSTNAME").ToString()
orgID = myReader("ORGA_ID").ToString()
End While
connection.Close()
Return "You are registered for this meeting"
End Function
When i remove the following it returns the string but otherwise it doesnt (compiles without errors either way):
While myReader.Read()
LastName = myReader("LASTNAME").ToString()
FirstName = myReader("FIRSTNAME").ToString()
orgID = myReader("ORGA_ID").ToString()
End While
The problem seems to be coming from myReader.Read()??? I dont understand why

orgID = myReader("ORGA_ID").ToString()
You have defined orgId as Integer;
Dim orgID As Integer

Sorry every1 the problem was solved by itself over night. Still dont know what the problem was. LOL

Related

How to Search a String with spaces in between

i am new to VB. so when i run my program and search for the name . i want to search with any spaces in between. For example i want to search " Brian Tracy" like that
i am quite confused on the string function that would do that. Thank you for your help
This is the code am working on
Dim search As String = txtAuthorsName.Text
'Search Sql For Authors Data
Dim conn As New
OleDb.OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
Server.MapPath("~/Access/bookstore.accdb"))
conn.Open()
Dim sql As String = "SELECT AID, authorName, authorSex FROM Authors"
Dim cmd As Object
If searchAuthor.SelectedValue.Equals("Name") Then
If search.Length > 0 Then
Dim keywords As String() = search.Split(" ")
sql = "SELECT * FROM Authors where authorName like '%" & keywords(0) & "%'"
For k As Integer = 1 To keywords.Length - 1
sql += " or authorName Like '%" & keywords(k) & "%'"
Next
End If
cmd = New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#authorName", "%" + txtAuthorsName.Text + "%")
Else
sql += "WHERE authorSex LIKE #authorSex"
cmd = New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#authorSex", "%" + txtAuthorSex.Text + "%")
End If
Dim dbread = cmd.ExecuteReader()
GridView2.DataSource = dbread
GridView2.DataBind()
dbread.Close()
conn.Close()
This is an example on how to filter/search data :
Public Sub FilterData(valueToSearch As String)
'
Dim searchQuery As String = "SELECT * From Users WHERE CONCAT(fname, lname, age) like '%" & valueToSearch & "%'"
Dim command As New SqlCommand(searchQuery, connection)
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
DataGridView1.DataSource = table
End Sub
Private Sub BTN_FILTER_Click(sender As Object, e As EventArgs) Handles BTN_FILTER.Click
FilterData(TextBox1.Text)
Make necessary changes, hope it works

ID numbers with text

Help. So this is my code and my database table. The itemcat is based on the tbl_category and it gives IN, PF, SS, SV + the number of the id.
As you can see the. PF003 and SS003 have the same number. How to change it to SS004 automatically?
Private Sub AutoGenerateID()
Dim mysqlconnection As MySqlConnection
Dim command As New MySqlCommand
mysqlconnection = New MySqlConnection()
mysqlconnection.ConnectionString = "Server=localhost;User=root;Password=;Database=debis"
command.Connection = mysqlconnection
mysqlconnection.Open()
da = New MySqlDataAdapter("select * from tbl_category where catname = '" & cmbcat.SelectedItem & "'", con)
ds.Reset()
da.Fill(ds)
Dim sqlquery = "select Max(itemid) from tbl_item "
command.CommandText = sqlquery
Dim ID As Integer
Dim value As String
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
value = command.ExecuteScalar().ToString()
If String.IsNullOrEmpty(value) Then
value = (ds.Tables(0).Rows(i).Item("catid")) + "0000"
End If
value = value.Substring(3)
Int32.TryParse(value, ID)
ID = ID + 1
value = (ds.Tables(0).Rows(i).Item("catid")) + ID.ToString("D3")
tbitemid.Text = value
Next
command.Dispose()
mysqlconnection.Close()
mysqlconnection.Dispose()
End Sub
This is the image of the table
If the first part is always 2 characters long then you can split your string to increment only the number part.
intValue = val(strings.mid(value,3))
intValue = intValue + 1
Also, you should hide/change your server address, user/password when you post your code on the web.

VB NET - Select * From issue

I have some wierd exception.
I have Access database with 2 tables, one named MYSB_DB and the other Employee.
I'm using the code below when I want to filter on something like that:
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String = String.Empty '
Dim pName As String = String.Empty
Dim fName As String = String.Empty
Dim ColpName As String = String.Empty
Dim ColfName As String = String.Empty
Dim ch As Integer = InStr(1, cmbEmployees.Text, " ", CompareMethod.Text)
pName = cmbEmployees.Text.Substring(0, ch)
fName = cmbEmployees.Text.Substring(ch, cmbEmployees.Text.Length - ch)
ColpName = "שדה1"
ColfName = "שדה2"
sql = "SELECT * FROM Employee WHERE [" & ColpName & "]=" & pName & ";"
da = New OleDb.OleDbDataAdapter(sql, Me.EmployeeTableAdapter.Connection.ConnectionString)
da.Fill(ds, Me.MYSB_DataBaseDataSet1.Employee.TableName)
I only change between the tables name in the code.
When I'm using this code for MYSB_DB table, the code is running well, but when I'm using the code for Employee table, I have an exception.
Any ideas why it's happening?
pName is a string, so it must be quoted:
sql = "SELECT * FROM Employee WHERE [" & ColpName & "]='" & pName & "';"

VB.net search button

I was working on this search button and I've manage to make it work. The only down side to this is when I try to search for a different item it keeps on putting back the old one I've searched for. What could be the problem?
Here's the code:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
com.Connection = con
com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'")
com.CommandText = "SELECT * FROM Student"
OpenDB()
Dim SurName As String = ""
Dim MiddleName As String = ""
Dim FirstName As String = ""
Dim SAddress As String = ""
Dim Birthday As String = ""
Dim Age As String = ""
Dim Birthplace As String = ""
Dim SContactNumber As String = ""
Dim GradeYear As String = ""
Dim SchoolYear As String = ""
Dim ParentGaurdian As String = ""
Dim PGContact As String = ""
Dim PGAddress As String = ""
rdr = com.ExecuteReader
If rdr.Read = True Then
SurName = rdr("StudentLastName").ToString
MiddleName = rdr("StudentMiddleName").ToString
FirstName = rdr("StudentFirstName").ToString
SAddress = rdr("StudentAddress").ToString
Birthday = rdr("Birthday").ToString
Age = rdr("Age").ToString
Birthplace = rdr("BirthPlace").ToString
SContactNumber = rdr("StudentContactNumber").ToString
GradeYear = rdr("GradeYearLevel").ToString
SchoolYear = rdr("SchoolYear").ToString
ParentGaurdian = rdr("ParentName").ToString
PGContact = rdr("ParentContactNumber").ToString
PGAddress = rdr("ParentAddress").ToString
End If
con.Close()
com.Dispose()
rdr.Close()
txtSurName.Text = SurName
txtMiddleName.Text = MiddleName
txtFirstName.Text = FirstName
txtSAddress.Text = SAddress
txtBirthday.Text = Birthday
txtAge.Text = Age
txtBirthplace.Text = Birthplace
txtSContactNumber.Text = SContactNumber
txtGradeYear.Text = GradeYear
txtSchoolYear.Text = SchoolYear
txtParentGaurdian.Text = ParentGaurdian
txtPGContact.Text = PGContact
txtPGAddress.Text = PGAddress
End Sub
I've tried to play around with the rdr.close because I think that's the thing I've placed wrong.
Looks like your SQL query should be more like this:
'com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'")
'com.CommandText = "SELECT * FROM Student"
com.CommandText = "SELECT * FROM Student WHERE Student_ID = '" & txtSearch.Text & "'"
Remove below two lines..
com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'")
com.CommandText = "SELECT * FROM Student"
And instead use below..
com.CommandText = ("Select Student_ID from Student where Student_ID = #searchText")
com.Parameters.AddWithValue("#searchText", txtSearch.Text)

Update sqlite query not working in Visual Basic 2013

I am trying to insert row and update row (if row already exists), as following -
Dim con As SQLiteConnection
Dim sql As String
Dim cmd As New SQLiteCommand
Dim da As SQLiteDataAdapter
Dim ds As New DataSet
Dim NumberOfRows As Integer
Public Sub UpdateUserStatistics(ByVal UserId As Integer, ByVal QId As Integer, _
ByVal KeyName As String, ByVal KeyValue As Integer)
con = New SQLiteConnection("Data Source = " + AppDomain.CurrentDomain.BaseDirectory + "/user_statistics.db;Version=3;")
con.Open()
sql = "SELECT * FROM med_user_meta where user_id=" & UserId & " And qid=" & QId
da = New SQLiteDataAdapter(sql, con)
da.Fill(ds, "UserMeta")
NumberOfRows = ds.Tables("UserMeta").Rows.Count
ds.Clear()
If (NumberOfRows = 0) Then
Dim InsertQuery As SQLiteCommand = con.CreateCommand
InsertQuery.CommandText = "INSERT INTO med_user_meta " _
& "(user_id, qid, " _
& KeyName _
& ", timestamp) VALUES(?, ?, ?, datetime('now'))"
InsertQuery.Parameters.AddWithValue("user_id", UserId)
InsertQuery.Parameters.AddWithValue("qid", QId)
InsertQuery.Parameters.AddWithValue(KeyName, KeyValue)
RowInserted = InsertQuery.ExecuteNonQuery()
ElseIf (NumberOfRows = 1) Then
Dim UpdateQuery As SQLiteCommand = con.CreateCommand
UpdateQuery.CommandText = "UPDATE med_user_meta SET " _
& KeyName _
& " = ?, timestamp = datetime('now') Where qid = ?"
'UpdateQuery.Parameters.AddWithValue("user_id", UserId)
UpdateQuery.Parameters.AddWithValue("qid", QId)
UpdateQuery.Parameters.AddWithValue(KeyName, KeyValue)
RowUpdated = UpdateQuery.ExecuteNonQuery()
End If
con.Close()
End Sub
I am using this code in a module and calling UpdateUserStatistics wherever required
My problem is that, insert query works fine, but update query does Not work.
It's been more than 2 hours, but I couldn't find, where am I making mistake?
Also, is there any way to get the final update query into message box after adding parameters, so that I can check if my final update query is correct?
Edit - 1
I forgot to mention, value of RowUpdated is returning 0, i.e. ExecuteNonQuery is Not updating the row.
I have also confirmed that row corresponding to update query Do exists, and the ElseIf (NumberOfRows = 1) Then block is running too, but it's Not updating the row.