I've been working on this one since someone teaches me to use gridview to display my search result.
My problem is, I can't even make it work, when I click or hit the search button, nothing happen. I have:
-1 textbox for last name
-2 dropdownlist for the province and city
-and a search(trigger)button
Here's what I've done so far:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_province ORDER BY PROVINCE_NAME ASC", conn);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
}
ddlProvince.DataSource = dt;
ddlProvince.DataTextField = "PROVINCE_NAME";
ddlProvince.DataValueField = "PROVINCE_CODE";
ddlProvince.DataBind();
}
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_city WHERE PROVINCE_CODE =#pcode", conn);
comm.Parameters.AddWithValue("#pcode", ddlProvince.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "#pcode";
param.Value = ddlProvince;
comm.Parameters.Add(param);
}
ddlCity.DataSource = dt;
ddlCity.DataTextField = "CITY_NAME";
ddlCity.DataValueField = "CITY_CODE";
ddlCity.DataBind();
}
private void BindGridView(string field)
{
DataTable dt = new DataTable();
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
try
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_accredited_providers WHERE DOCTOR_CODE =#pcode", conn);
comm.Parameters.AddWithValue("#pcode", ddlProvince.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "#pcode";
param.Value = ddlProvince;
comm.Parameters.Add(param);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
}
// NO RECORDS FOUND
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGridView(txtName.Text.Trim());
}
}
I'm new to this, please assist me. Thanks!
You are not using the field string variable that you are passing to BindGridView and you are mismanaging your SQL parameters (adding the same parameter twice and assigning a DropDown object as a parameter value).
You are adding the same parameter twice.
To fix this, remove this line: comm.Parameters.AddWithValue("#pcode", ddlProvince.SelectedValue);
You are not using the field variable.
To fix this, change this line
param.Value = ddlProvince; // Note: You are assigning a dropdown OBJECT as the value here!
to
param.Value = field;
in your BindGridView function.
Related
private void Button_Click(object sender, RoutedEventArgs e)
{
SqlConnection sc = new SqlConnection();
SqlCommand com = new SqlCommand();
sc.Open();
com.Connection = sc;
string sql;
{
sql = "SELECT FROM WolfAcademyForm WHERE [Forename] == 'txtSearch.Text';";
{
grdSearch.ItemsSource = sql;
sc.Close();
}
This is the code that I have, When I press the search button nothing shows up... Can someone please help me with this problem, I don't get any errors
Problems:
SQL query is not right:
It should be like SELECT * FROM TABLENAME.
In WHERE clause [Forename] == 'txtSearch.Text', == should = and Textbox value should be concatenated using +.
Fixed Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
string sConn = #"Data Source=MYDS;Initial Catalog=MyCat;
User ID=MyUser;Password=MyPass;";
using(SqlConnection sc = new SqlConnection(sConn))
{
sc.Open();
string sql = "SELECT * FROM WolfAcademyForm WHERE [Forename]= #Forename";
SqlCommand com = new SqlCommand(sql, sc);
com.Parameters.AddWithValue("#Forename", txtSearch.Text);
using(SqlDataAdapter adapter = new SqlDataAdapter(com))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
grdSearch.ItemsSource = dt.DefaultView;
}
}
}
Use this
using (SqlConnection con = new SqlConnection(ConString))
{
CmdString = "SELECT FROM WolfAcademyForm WHERE [Forename] == " + txtSearch.Text + ";"
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Employee");
sda.Fill(dt);
grdSearch.ItemsSource = dt.DefaultView;
}
I using below code but it's not showing name in textbox.\
private void AllotLeaves_Load(object sender, EventArgs e)
{
display();
}
private void display()
{
SqlConnection conn = new SqlConnection(#"Data Source=.......");
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter("select *from Emp_Details", conn);
DataSet ds = new DataSet();
ad.Fill(ds, "Emp_Details");
cballotid.DataSource = ds.Tables["Emp_Details"].DefaultView;
cballotid.DisplayMember = "ID";
cballotid.ValueMember = "ID";
}
private void cballotid_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(#"Data Source=......");
conn.Open();
SqlCommand command = new SqlCommand("select Name from Emp_Details where ID=#ID", conn);
command.Parameters.AddWithValue("#ID", cballotid.Text);
Object temp = command.ExecuteScalar(); // this returns the first value of the select statement.
if (temp != null)
txtallotname.Text = temp.ToString();
conn.Close();
}
Try this
cballotid.DisplayMember = "Name";
cballotid.ValueMember = "ID";
cballotid.BindingContext = this.BindingContext;
Reference: ListControl.DisplayMember Property
You have to change this
cballotid.DisplayMember = "ID";
cballotid.ValueMember = "ID";
to
cballotid.DisplayMember = "name of field youd like to display";
cballotid.ValueMember = "ID";
Change this line as below:-
command.Parameters.AddWithValue("#ID", cballotid.SelectedValue);
I am trying to create a search from ProductDB(database), the main columns I would like the user to search is Material_No and Product_Line.
So far, I have the following:
Drop Down List:
<asp:DropDownList ID="DropDownList" runat="server" Height="16px"
onclick="SearchButton_Click" Width="144px"
AutoPostBack="True">
<asp:ListItem>Please select...</asp:ListItem>
<asp:ListItem Value="0">Material No</asp:ListItem>
<asp:ListItem Value="1">Product Line</asp:ListItem>
</asp:DropDownList>
TextBox:
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged">
</asp:TextBox>
Search Button:
<asp:Button ID="SearchButton" runat="server" Text="Search"
onclick="SearchButton_Click" />
So I am trying to do is when the user chooses either Material No or Product Line when he types the Material No or Product Line after clicking the search button, the result should show either in grid format or something similar, and if he just clicks search without choosing anything all the result should show.
Here is what I have done so far.
Old Code:
protected void SearchButton_Click(object sender, EventArgs e)
{
string Selectedvalue = DropDownList.SelectedItem.Value;
if (DropDownList.SelectedItem.ToString() == "Material No")
{
MessageBox.Show("Material No selected");
string textbox = TextBox1.Text;
MessageBox.Show(textbox.ToString());
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
DataSet dsData = new DataSet();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM ProductDB WHERE Material_No ='" + TextBox1.Text + "'";
cmd.Connection = conn;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("", conn);
SqlCommandBuilder cmdBldr = new SqlCommandBuilder(da);
da.Fill(dsData, TextBox1.Text);
MessageBox.Show("Connection Successful");
conn.Close();
}
else
{
MessageBox.Show("Product Line selected");
}
}
New Code:
private SqlConnection conn;
private SqlDataAdapter daMaterial;
private SqlDataAdapter daProduct;
private SqlCommand cmdMaterial;
private SqlCommand cmdProduct;
private SqlParameter paramMaterial;
private SqlParameter paramProduct;
private DataSet dsMaterial;
private DataSet dsProduct;
private DataGrid dgMaterial;
private DataGrid dgProduct;
private const string tableNameMaterial = "Material_No";
private const string tableNameProduct = "Product_Line";
enter code here
protected void SearchButton_Click(object sender, EventArgs e)
{
string Selectedvalue = DropDownList.SelectedItem.Value;
if (DropDownList.SelectedItem.ToString() == "Material No")
{
//MessageBox.Show("Material No. Selected");
string textbox = TextBox1.Text;
//MessageBox.Show(textbox.ToString());
conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
dsMaterial = new DataSet();
daMaterial = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Material_No = #Material_No", conn);
daMaterial.SelectCommand.CommandText = "SELECT * FROM ProductDB WHERE Material_No = #Material_No";
paramMaterial = new SqlParameter();
paramMaterial.ParameterName = "#Material_No";
paramMaterial.Value = TextBox1.Text;
daMaterial.SelectCommand = cmdMaterial;
cmdMaterial.Parameters.Add("#Material_No", SqlDbType.BigInt).Value = TextBox1.Text;
daMaterial.Fill(dsMaterial, tableNameMaterial);
//MessageBox.Show("Connection Successful");
conn.Close();
}
else
{
//MessageBox.Show("Product Line selected");
string textbox = TextBox1.Text;
//MessageBox.Show(textbox.ToString());
conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
dsProduct = new DataSet();
daProduct = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Product_Line = #Product_Line", conn);
daProduct.SelectCommand.CommandText = "SELECT * FROM ProductDB WHERE Product_Line = #Product_Line";
paramProduct = new SqlParameter();
paramProduct.ParameterName = "#Product_Line";
paramProduct.Value = TextBox1.Text;
daProduct.SelectCommand = cmdProduct;
cmdProduct.Parameters.Add("#Product_Line", SqlDbType.VarChar, 50).Value = TextBox1.Text;
conn.Open();
daProduct.Fill(dsProduct, tableNameProduct);
//MessageBox.Show("Connection Successful");
conn.Close();
}
}
I am getting an error "Object reference not set to an instance of an object."
Can someone check whether the Parameter use is correct with the SqlDataAdapter
protected void btnSearch_Click(object sender, EventArgs e)
{
string Query = string.Empty;
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
if (DropDownList1.SelectedValue.ToString() == "Name")
{
Query = "select * from tbl_Employee where Name Like '" + txtSearchName.Text + "%'";
}
else if (DropDownList1.SelectedValue.ToString() == "Designation")
{
Query = "select * from tbl_Employee where Designation Like '" + txtSearchName.Text + "%'";
}
SqlDataAdapter sqlDa = new SqlDataAdapter(Query, sqlCon);
DataSet Ds = new DataSet();
sqlDa.Fill(Ds);
dgvEmployee.DataSource = Ds;
dgvEmployee.DataBind();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("<script>alert('wfrmGrid: 11')</script>" + ex.Message);
}
}
Some things:
daProduct = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Product_Line = #Product_Line", conn);
Creates a new SqlDataAdapter and initializes it's SelectCommand with the CommandText and Connection, hence following lines are redundant and inherently error-prone:
daProduct.SelectCommand.CommandText = "SELECT * FROM ProductDB WHERE Product_Line = #Product_Line";
daProduct.SelectCommand = cmdProduct;
The second instruction even overrides the first with a new CommandText and Connection without ever having used.
paramProduct = new SqlParameter();
Instead of using this parameterless constructor i would use AddWithValue or Parameters.Add which are less error-prone(e.g. you haven't provide a type).
cmdProduct.Parameters.Add( ....
Now you are using the method i've suggested without ever having used paramProduct.
You're also never disposing unmanaged resources(e.g. connections are staying open in case of errors), use the using-statement therefore. Btw, if you use a DataAdapter you don't even need to open/close the connection, that is done implicitely on DataAdapter.Fill.
Sorry, but your code is a mess.Instead of fiddling around with yours, i'll provide a clean version that should work.
....
else
{
using(var con = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True"))
using (var daProduct = new SqlDataAdapter("SELECT * FROM ProductDB WHERE Product_Line = #Product_Line", con))
{
daProduct.SelectCommand.Parameters.Add("#Product_Line", SqlDbType.VarChar, 50).Value = TextBox1.Text;
dsProduct = new DataSet();
daProduct.Fill(dsProduct, "Product_Line");
}
}
The following code is working:
private const string tableNameMaterial = "Material_No";
private const string tableNameProduct = "Product_Line";
protected void SearchButton_Click(object sender, EventArgs e)
{
string Selectedvalue = DropDownList.SelectedItem.Value;
if (DropDownList.SelectedItem.ToString() == "Material No")
{
//MessageBox.Show("Material No. Selected");
string textbox = TextBox1.Text;
//MessageBox.Show(textbox.ToString());
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM ProductDB WHERE Material_No = #Material_No";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#Material_No", SqlDbType.BigInt);
cmd.Parameters["#Material_No"].Value = TextBox1.Text;
SqlDataAdapter daMaterial = new SqlDataAdapter();
daMaterial.SelectCommand = cmd;
DataSet dsMaterial = new DataSet();
conn.Open();
daMaterial.Fill(dsMaterial, tableNameMaterial);
//MessageBox.Show("Connection Successful");
GridView1.DataSource = dsMaterial;
GridView1.DataBind();
conn.Close();
}
else
{
//MessageBox.Show("Product Line selected");
string textbox = TextBox1.Text;
//MessageBox.Show(textbox.ToString());
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM ProductDB WHERE Product_Line = #Product_Line";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#Product_Line", SqlDbType.VarChar);
cmd.Parameters["#Product_Line"].Value = TextBox1.Text;
SqlDataAdapter daProduct = new SqlDataAdapter();
daProduct.SelectCommand = cmd;
DataSet dsProduct = new DataSet();
conn.Open();
daProduct.Fill(dsProduct, tableNameProduct);
//MessageBox.Show("Connection Successful");
GridView1.DataSource = dsProduct;
GridView1.DataBind();
conn.Close();
}
}
Now, I need the search box to autocomplete the letters from the database... if anyone has any suggestion, please do share it.
Thank you
I recieve the following error when I try to execute this code. But I added it to my commands. Can someone point out the step that overlooked? Thanks.
Procedure or function 'usps_getContactDetails' expects parameter '#aspContactID', which was not supplied.
SqlConnection conn = new SqlConnection(GetConnString());
SqlCommand cmd = new SqlCommand("usps_getContactDetails", conn);
SqlParameter parmContactID = new SqlParameter("#aspContactID", Convert.DBNull);
cmd.Parameters.Add(parmContactID);
parmContactID.Direction = ParameterDirection.Input;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
conn.Open();
DataSet cusDS = new DataSet();
da.Fill(cusDS, "Contacts");
When doing a SqlCommand and calling a stored procedure, you need to implicity set your SqlCommand to be a StoredProcedure.
using(SqlConnection con = new SqlConnection(""))
{
//Set up your command
SqlCommand cmd = new SqlCommand("[Procedure]", con);
cmd.CommandType = CommandType.StoredProcedure;
//Add your parameters
cmd.Parameters.AddWithValue("#aspContactID", "");
//Declare your data adapter
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "Contacts");
}
Follow the above format and you should be fine. Your procedure isn't working for one of two reasons, you were missing the line of code which makes your code work in this case is cmd.CommandType = CommandType.StoredProcedure; or because your parameter is DBNull the procedure says it doesn't have any recognition of that parameter. If you have a parameter which can be null or empty in a stored procedure then do the following:
Create Procedure [dbo].[Example]
#Test as Varchar(100) = ''
As
protected void Page_Load(object sender, EventArgs e)
{
}
private void OpenCon()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbPrepConnectionString"].ConnectionString.ToString());
try
{
con.Open();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
private void SubmitData()
{
OpenCon();
string sp = "sp_InsertRecord";
cmd = new SqlCommand(sp, con);
cmd.CommandType = CommandType.StoredProcedure;
//add parameters...
cmd.Parameters.Add(new SqlParameter("#Name", SqlDbType.VarChar, 50));
cmd.Parameters.Add(new SqlParameter("#UserId", SqlDbType.Int));
cmd.Parameters.Add (new SqlParameter ("#ProductName",SqlDbType .VarChar,50));
cmd.Parameters.Add(new SqlParameter("#Price", SqlDbType.Money));
//set paarameters....
cmd.Parameters["#Name"].Value = txtName.Text.ToString();
cmd.Parameters["#UserId"].Value = txtUserId.Text.ToString();
cmd.Parameters["#ProductName"].Value = txtProductName.Text.ToString();
cmd.Parameters["#Price"].Value = txtPrice.Text.ToString();
cmd.ExecuteNonQuery();
lblMessage.Text = "data inserted successfully";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SubmitData();
}
private void FindData()
{
OpenCon();
string s = "sp_FindRecord";
cmd = new SqlCommand(s, con);
cmd.Parameters.Add(new SqlParameter("#Id", SqlDbType.Int));
cmd.Parameters["#Id"].Value = txtName.Text.ToString();
cmd.CommandType = CommandType.StoredProcedure;
ad = new SqlDataAdapter(cmd);
ds = new DataSet();
ad.Fill(ds);
dt = ds.Tables[0];
currow = 0;
FillControls();
}
private void FillControls()
{
txtOrderId.Text = dt.Rows[currow].ItemArray[0].ToString();
txtUserId.Text = dt.Rows[currow].ItemArray[1].ToString();
txtProductName.Text = dt.Rows[currow].ItemArray[2].ToString();
txtPrice.Text = dt.Rows[currow].ItemArray[3].ToString();
}
protected void btnFind_Click(object sender, EventArgs e)
{
FindData();
}
}
}'
I'm just starting out at asp.net c# and I have been given a task to generate the available doctors upon the given values in a drop-down list.
I have 3 drop-down lists, (1)PROVINCE, (2)CITY, (3)SPECIALIZATION and a search button.
After the user selects the values of 3 drop-down lists and hits the search button it will print a table containing the available doctor.
I know that the key is on the search button, but I don't exactly know what to put under the search button. Can you help me out please?
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
if (!IsPostBack)
{
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_province ORDER BY PROVINCE_NAME ASC", conn);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
}
ddlProvince.DataSource = dt;
ddlProvince.DataTextField = "PROVINCE_NAME";
ddlProvince.DataValueField = "PROVINCE_CODE";
ddlProvince.DataBind();
}
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_city WHERE PROVINCE_CODE =#pcode", conn);
comm.Parameters.AddWithValue("#pcode", ddlProvince.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "#pcode";
param.Value = ddlProvince;
comm.Parameters.Add(param);
}
ddlCity.DataSource = dt;
ddlCity.DataTextField = "CITY_NAME";
ddlCity.DataValueField = "CITY_CODE";
ddlCity.DataBind();
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_city");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("select distinct emed_accredited_providers.SPECIALIZATION from emed_accredited_providers inner join emed_doctors_hospitals on emed_accredited_providers.DOCTOR_CODE = emed_doctors_hospitals.DOCTOR_CODE where CITY_CODE =#ccode", conn);
comm.Parameters.AddWithValue("#ccode", ddlCity.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "#ccode";
param.Value = ddlCity;
comm.Parameters.Add(param);
}
ddlSpec.DataSource = dt;
ddlSpec.DataTextField = "SPECIALIZATION";
ddlSpec.DataValueField = "SPECIALIZATION";
ddlSpec.DataBind();
}
protected void btnDocs_Click(object sender, EventArgs e)
{
}
}
}
string query = string.Empty;
if (ddlProvince.SelectedIndex != -1)
{
query = query + " and PROVINCE_CODE=" + ddlProvince.SelectedValue;
}
if (ddlCity.SelectedIndex != -1)
{
query = query + " and CITY_CODE=" + ddlProvince.SelectedValue;
}
if (ddlSpec.SelectedIndex != -1)
{
query = query + " and SPECIALIZATION=" + ddlProvince.SelectedValue;
}
string tQuery = "Select * from Doc";
if (query.Length > 0)
{
query = query.Remove(0, 4);
tQuery = tQuery + query;
}
// Exeucate -- tQuery
// Modify table name or field name.
get the selected values from the drop down list
pass the selected values to your search function. (It depends on your logic, I mean if user select values from only one drop down, from only two drop down your search query may differ).
get the return values from your search function and bind it to your table (grid view)
Also have a look at the DropDownList.SelectedIndex Property
to bind the gridview you can do something similar to this
private void BindGrid ()
{
var dt = new DataTable();
var connection = new SqlConnection(YOUR CONNECTION);
try
{
connection.Open();
var query = "YOUR SEARCH QUERY";
var sqlCmd = new SqlCommand(query, connection);
var sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
//
}
finally
{
connection.Close();
}
}