'How to display gridview by using a table as condition then using select query to do the filter
I am new of the C# asp.net. I have two tables. The first can be a setup table then second one is a table that wants to do a filter. I want use the setup table as the condition to search the second table then filter the result and display as gridview. As example, the setup table got "S","50","BU2". then they will be condition to search the second table by using Select query
protected void Run_Click(object sender, EventArgs e)
{
try
{
string site = string.Empty;
string operation = string.Empty;
string bu = string.Empty;
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
using (SqlConnection conn = new SqlConnection(Contest))
{
string query = string.Empty;
conn.Open();
query = "Select Site, Operation, BU from [dbo].[Sampling_test]";
SqlDataAdapter adp = new SqlDataAdapter(query, conn);
adp.Fill(dt);
}
for (int b = 0; b < dt.Rows.Count; b++)
{
site = dt.Rows[b]["Site"].ToString();
operation = dt.Rows[b]["Operation"].ToString();
bu = dt.Rows[b]["BU"].ToString();
string[] operationArray = operation.Split(',');
string operationQuery = string.Empty;
for (int a = 0; a < operationArray.Length; a++)
{
operationQuery = operationQuery + "" + operationArray[a] + ",";
}
operationQuery.TrimEnd(',');
using (SqlConnection conn = new SqlConnection(Contest))
{
string query = string.Empty;
conn.Open();
query = "Select * from [dbo].[JHtest] where Site = '" +site+ "' AND Operation in ('" +operationQuery+ "') AND BU = '" +bu+ "' ";
SqlDataAdapter adp = new SqlDataAdapter(query, conn);
adp.Fill(dt1);
}
//insert dt into database
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString))
{
conn.Open();
GridView2.DataSource = dt1;
GridView2.DataBind();
}
}
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
