I have written a code that searches specific string from database table.what i want to implement is search like when we search friends on facebook means it returns results matching to the input given even if it is partial
for example if i want to search ANDREW as soon as i enter single character results should start to appear if i enter “an” like
andy
andrew
and….
here is my code for textbox text changed method
table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Type");
table.Columns.Add("Status");
table.Columns.Add("Date Created");
table.Columns.Add("Action");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = textBoxSearch.Text;
cmd.CommandText = "SELECT id,uName,uType,uStatus,uDate from users WHERE uName=@username ";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
{
MessageBox.Show(dr["uName"].ToString() + dr["uType"].ToString() + dr["uStatus"].ToString());
row = table.NewRow();
row["Name"] = dr["uName"].ToString();
row["Type"] = dr["uType"].ToString();
row["Status"] = dr["uStatus"].ToString();
row["Date Created"] = dr["uDate"].ToString();
// row["Action"] = new Button();
table.Rows.Add(row);
UsersView.DataSource = table;
}//End While for entering peresent amount of data
}//End If to check wether or not users exist
dr.Close();//Close Datareader
}
As you’re using C#, why not go for something more LINQ-y?
Set up your LINQ Database Context file and use the following code to achieve what you’re trying to (quickly typing this, sorry if there’s any errors):