1

I am trying to get a simple function in my program to find data in a DB and bring it back. I am doing this using two text boxes. Its a translation function so when you search for one word in the first text box it should display the translated/matched word from the DB in the second texbox. Cant get it to work, heres my code anyway...

string strProvider = @"PATH is here";        
string strSql = "SELECT jpn FROM Vocab WHERE eng like '" + textBox1.Text + "%'";

OleDbConnection newConn = new OleDbConnection(strProvider);
OleDbCommand dbCmd = new OleDbCommand(strSql, newConn);

newConn.Open();
dbCmd.CommandType = CommandType.Text;
OleDbDataReader dr = dbCmd.ExecuteReader();

while (dr.Read())
{                 
    textBox2.Text = dr.GetString(0);             
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
JoshF91
  • 99
  • 1
  • 3
  • 11
  • What is your `strSql` looks like when you debug? What is the value of `textBox1.Text` exactly? And please use parameterized queries. This kind of string concatenations are open for _SQL Injection_ attacks. – Soner Gönül Mar 24 '14 at 15:13
  • And use [`using` statement](http://msdn.microsoft.com/en-us/library/yh598w02.aspx) to dispose your `OleDbConnection`, `OleDbCommand` and `OleDbDataReader`. – Soner Gönül Mar 24 '14 at 15:21
  • Please elaborate Can't get it to work? Does it give you an error or you just dont get the expected result? – SoulTrain Mar 24 '14 at 15:25
  • I noticed you aren't you using `like '% %'`, is that coz you have a defined pattern – SoulTrain Mar 24 '14 at 15:27
  • There is no error, just doesnt do anything, but when i leave textbox1 empty, it brings back for the first row from the DB and displays it. – JoshF91 Mar 24 '14 at 15:42
  • The value of textbox1 is a word that has a corresponding word in the same row (jpn and eng) – JoshF91 Mar 24 '14 at 15:43

0 Answers0