I am working on the simple project in Search of SQL Database. In the project I have: combox for display table names, listboxes for display of column names, data types, data view and Sql Query statments, and textbox with button for search any keyword.
Every time I type a word in the textbox like "rio" I am getting an error
ERROR: System.Data.SqlClient.SqlException: 'Incorrect syntax near '0'.'
I debugged that line of code but i see the SQL statement shows: SELECT 1 FROM 0 WHERE 1 LIKE %rio% ???
Please see the code below:
Form1.cs
private void TextSearchDB()
{
lstBoxDataViewInfo.Items.Clear();
int tablecount = cmbBoxTableName.Items.Count;
for(int i=0;i<tablecount;i++)
{
cmbBoxTableName.SelectedIndex = i;
int columncount = lstBoxColumnName.Items.Count;
for(int j=0;j<columncount;j++)
{
lstBoxColumnName.SelectedIndex = j;
lstBoxDataType.SelectedIndex = j;
string tablename = cmbBoxTableName.SelectedIndex.ToString();
string columnname = lstBoxColumnName.SelectedIndex.ToString();
string datatype = Parseinput(textBoxSearchFullDB.ToString());
if (lstBoxDataType.SelectedItem.ToString() == datatype)
{
if (datatype != "int")
{
sqlcmd.CommandText = "SELECT " + columnname.ToString() + " FROM " + tablename.ToString() + " WHERE " + columnname.ToString() + " LIKE '%" + textBoxSearchFullDB.Text.ToString() + "%'";
}
if (datatype == "int")
{
sqlcmd.CommandText = "SELECT " + columnname.ToString() + " FROM " + tablename.ToString() + " WHERE " + columnname.ToString() + " LIKE '%" + textBoxSearchFullDB.Text.ToString() + "%'";
}
sqlconn.Open();
Error --->
sqldr = sqlcmd.ExecuteReader(); //ERROR: System.Data.SqlClient.SqlException: 'Incorrect syntax near '0'.'
if (sqldr.HasRows)
{
while (sqldr.Read())
{
lstBoxDataViewInfo.Items.Add(sqldr[0].ToString() + " Column Name: " + columnname.ToString() + " FROM TABLE:" + tablename.ToString());
Application.DoEvents();
lstBoxSQLQueryStatement.Items.Add(sqlcmd.CommandText.ToString());
}
}
sqlconn.Close();
}
}
}
Your help is much appreciated. Thanks.