Goofy Posted January 24, 2003 Posted January 24, 2003 Ok this is the situation. I need to find some key word from my tables row called "NameID". I think i must somehow tell to vb.net that there is a row called "NameID" am i rigth? but the code would be something like this. 'TableName = variable of currents table name TextBox1.Text = KeyWord Dim da as new SqlDataAdapter ("Select * from" & TableName & Where NameID = % & KeyWord & %, MyConnString) '% because the keyword can start or end diccerence like Lotr keyword will find Lotr2 and The Lotr2... da.Fill(MyDataSet, TableName) I cant even run this because it gives error about that NameID. I think im close to right? :) Quote
*Experts* Bucky Posted January 24, 2003 *Experts* Posted January 24, 2003 Yes, you're very close, you just forgot to put quote marks properly around strings to differentiate them from variables or other keywords. Assuming TableName and KeyWord are variables, your code should look like this, with quotes around the actual SQL statements: Dim da As New SqlDataAdapter ("Select * from" & TableName & "Where NameID = %" & KeyWord & "%", MyConnString) Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Experts* Nerseus Posted January 25, 2003 *Experts* Posted January 25, 2003 Closer... try this: Dim da As New SqlDataAdapter ("Select * from [" & TableName & "] Where NameID LIKE '%" & KeyWord.Replace("'", "''") & "%'", MyConnString) I put brackets around the TableName in case it's a reserved word. I change "NameID = ..." to "NameID LIKE ..." so that your % search would work. I also wrapped the KeyWord with single quotes (SQL standard for strings) and replaced one single quote with two, in case KeyWord has a value like "O'Malley" which would generate a SQL error. Keep in mind that putting the % BEFORE the keyword will cause a tablescan - could be very VERY slow if the table is large. Otherwise it's just slow :) If you can, only use the % at the end which won't be nearly as bad. It's fairly standard to assume that you must type the *first* few letters when doing searches in applications. Doing "full text searches" aren't strange, but less common as they will cause the aforementioned slowness :) -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Goofy Posted January 27, 2003 Author Posted January 27, 2003 Thanks. works fine now. Dim da As New SqlDataAdapter("Select * from [" & TableName & "] Where NameID LIKE '" & KeyWord.Replace("'", "''") & "%'", myconnstring) da.Fill(MyDataSet, TableNAme Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.