相关文章推荐
温柔的保温杯  ·  Spring Cloud实战 | ...·  10 月前    · 
任性的筷子  ·  Java中List ...·  1 年前    · 
重感情的手术刀  ·  create table from ...·  1 年前    · 
I am already okay with the checking when adding and it works just as I wanted it to be.
I used the oledbDataReader to check my database
This is my sample code used when checking the database (This code is for when I am adding new entry)
Dim CompQuery As String = " SELECT * FROM Company WHERE CompanyName = '" & txtCompName.Text & " ' AND AssociationID = " & cmbUnderAssoc.SelectedValue & " " Dim compCommand As OleDbCommand = New OleDbCommand(CompQuery, con) compCommand.Parameters.AddWithValue( " CompanyName" , txtCompName.Text) compCommand.Parameters.AddWithValue( " AssociationID" , cmbUnderAssoc.SelectedValue) con.Close() con.Open() Using compReader As OleDbDataReader = compCommand.ExecuteReader If compReader.HasRows Then MsgBox( " Entry already exist! Please input new entry" ) Exit Sub I tried the same logic when updating but it blocks even the entry that is being edited. So I need to change Name of the Company or its Association in order for it to update.
What I want to happen is that the entry being edited can be updated anytime even without changes being made while the datareader will compare the entry to the other entries in the database to check whether it will have any duplicate entry upon updating.
Thanks in advance :)

Um...when you were reading up on parameterised queries, did you notice how you were supposed to use them?
This query:


Dim CompQuery As String = " SELECT * FROM Company WHERE CompanyName = '" & txtCompName.Text & " ' AND AssociationID = " & cmbUnderAssoc.SelectedValue & " " does not use parameters. Instead, it uses the text directly and is wide open to SQL injection.
Try this:
Dim CompQuery As String = " SELECT COUNT(*) FROM Company WHERE CompanyName = ? AND AssociationID = ?" Dim compCommand As OleDbCommand = New OleDbCommand(CompQuery, con) compCommand.Parameters.AddWithValue( " ?" , txtCompName.Text) compCommand.Parameters.AddWithValue( " ?" , cmbUnderAssoc.SelectedValue) con.Close() con.Open() If Convert.ToInt32(compCommand.ExecuteScalar()) > 0 Then MsgBox( " Entry already exist! Please input new entry" ) Exit Sub Update [ ^ ] statement is used to update record(s) based on criteria. So, it can't create duplicates!
UPDATE TableName SET FieldName = " Whatever" WHERE <Condition>
Finally, your query should look like:
Dim CompQuery As String = " UPDATE * Company" & vbCr & _ " SET CompanyName = '" & txtCompName.Text & " ' AND AssociationID = " & cmbUnderAssoc.SelectedValue & vbCr & _ " WHERE CompanyId = " & txtComapnyId.Text & " "
Note: i use CompanyId as Primary Key [ ^ ] of Company table.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •