I have a table in sql server database and have some columns.
I want to search it via a text box and here is the scenario:
For example,there are 3 columns named: " name , number , code"
I want to type number 88 in the text box and after hitting search button,Every row that have a cell containing 88 will be shown to me in grid view.(and the 88 is a part of that cell,for example,there is a cell with 2256887 in it,and I want that cell to be shown)
I tried to solve,but there are two problems.first is: I can only search one column at a time,and second,if I even type 225688 , it will not show me anything until I type the whole 2256887 in the text box.
Here is the code in aspx page:
<asp:Label ID="Label1" runat="server" Text="متن ورودی برای جستجو:"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="search" OnClick="btnSearch_Click" />
<asp:GridView ID="searchresults" runat="server"></asp:GridView>
and the code in page cs:
using System.Data;
using System.Data.SqlClient;
protected void btnSearch_Click(object sender, EventArgs e)
{
String strConn = "Data Source=DESKTOP-MQ1PNVA\\SQLEXPRESS;Initial Catalog=linkfinderdb;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlCommand cmd = new SqlCommand("Select * FROM linktest WHERE phone=@txtSearch", conn);
try
{
SqlParameter search = new SqlParameter();
search.ParameterName = "@txtSearch";
search.Value = txtSearch.Text.Trim();
cmd.Parameters.Add(search);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
searchresults.DataSource = dt;
searchresults.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
phone is a column name in table.
Thanks in advance.