Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with SQL statement

Status
Not open for further replies.

MikeL91

Programmer
Feb 8, 2001
100
US
This code will not work, it says unhandeled exception.

[red] Private Sub btnState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnState.Click
Me.OleDbSelectCommand1.CommandText = "SELECT Name, [Company Name], Address, City, State, Zip, Telephone, PubID FROM Pub" & _
"lishers WHERE Name like " & cmbState.Text.ToString
Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
'
OleDbDataAdapter1.Fill(DataSet11)
OleDbDataAdapter2.Fill(DataSet11)
End Sub [/red]



This code works, it pulls everything in my table:

[green] Private Sub btnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAll.Click
Me.OleDbSelectCommand1.CommandText = "SELECT Name, [Company Name], Address, City, State, Zip, Telephone, PubID FROM Pub" & _
"lishers"
Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
'
OleDbDataAdapter1.Fill(DataSet11)
OleDbDataAdapter2.Fill(DataSet11)
[/green]
 
check the Where clause:

Code:
Me.OleDbSelectCommand1.CommandText = "SELECT Name, [Company Name], Address, City, State, Zip, Telephone, PubID FROM Pub" & _
        "lishers WHERE Name like '%" & cmbState.Text.ToString & "%'"

-Rick

----------------------
 
no that still does not work. here is the exact error message:

[red]
An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll

Additional information: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
[/red]
 
Not sure on that one. It sounds like some sort of data issue, but that shouldn't cause a problem with a select statement, unless the bad data is already in the table. You may have to go through the table manually and find the record the is violating the constraint.

-Rick

----------------------
 
Thanks for your help anyway, I guess I'll keep playing with it. The Select * command works though, thasts whats wierd, so I don't suspect the data, but I am new to .net.

It should be in a stored proc. but im using access DB, so I thought this would be a way around that.
 
The first question is this: do you have any duplicate PubID's?
 
Okay, popped open access, the like syntax should be as follows:

"WHERE Name like " & controlchars.Quote & "*" & cmbState.Text.ToString & "*" & controlchars.Quote

That should give you:
WHERE Name Like "*value*"

-Rick

----------------------
 
Is it a NULL problem? Not sure what happens with LIKE and nullable columns...

Try
Code:
WHERE Name is not null and Name like '%" & cmbState.Text.ToString & "%'"
and see what happens.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top