RichardSa says:
protected
void
Recsearch_Click(
object
sender, EventArgs e)
{
if
(ddlTables.SelectedIndex > 0)
{
string
query =
""
;
if
(ddlTables.SelectedValue.ToLower() ==
"Team"
)
{
query =
"SELECT Uid,email,Role,Name,CreateDate FROM Users WHERE (CreatedBy=@CreatedBy OR CreatedBy = '')"
;
if
(!
string
.IsNullOrEmpty(TextBox1.Text) && !
string
.IsNullOrEmpty(Dat.Text))
{
query +=
" AND CreateDate BETWEEN @From AND @To"
;
}
}
if
(ddlTables.SelectedValue.ToLower() ==
"cards"
)
{
query =
"SELECT Uid,image,CreatedBy,Role,CreatedDate FROM CardTbl WHERE (CreatedBy=@CreatedBy OR CreatedBy = '')"
;
if
(!
string
.IsNullOrEmpty(TextBox1.Text) && !
string
.IsNullOrEmpty(Dat.Text))
{
query +=
" AND CreatedDate BETWEEN @From AND @To"
;
}
}
if
(ddlTables.SelectedValue.ToLower() ==
"certificates"
)
{
query =
"SELECT Uid,image,CreatedBy,Role,CreatedDate FROM CertTbl WHERE (CreatedBy=@CreatedBy OR CreatedBy = '')"
;
if
(!
string
.IsNullOrEmpty(TextBox1.Text) && !
string
.IsNullOrEmpty(Dat.Text))
{
query +=
" AND CreatedDate BETWEEN @From AND @To"
;
}
}
if
(ddlTables.SelectedValue.ToLower() ==
"reciepts"
)
{
query =
"SELECT Uid,image,CreatedBy,Name,CreatedDate FROM tablereceipt WHERE (CreatedBy=@CreatedBy OR CreatedBy = '')"
;
if
(!
string
.IsNullOrEmpty(TextBox1.Text) && !
string
.IsNullOrEmpty(Dat.Text))
{
query +=
" AND CreatedDate BETWEEN @From AND @To"
;
}
}
DataTable dt =
new
DataTable();
using
(SqlConnection con =
new
SqlConnection(
"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"
))
{
using
(SqlCommand cmd =
new
SqlCommand(query))
{
using
(SqlDataAdapter sda =
new
SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue(
"@CreatedBy"
, createby.Text);
if
(!
string
.IsNullOrEmpty(TextBox1.Text) && !
string
.IsNullOrEmpty(Dat.Text))
{
cmd.Parameters.AddWithValue(
"@From"
, TextBox1.Text);
cmd.Parameters.AddWithValue(
"@To"
, Dat.Text);
}
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
Please refer below modified code.
protected void Recsearch_Click(object sender, EventArgs e)
{
if (ddlTables.SelectedIndex > 0)
{
string query = "";
if (ddlTables.SelectedValue.ToLower() == "Team")
{
query = "SELECT Uid,email,Role,Name,CreateDate FROM Users WHERE (CreatedBy=@CreatedBy OR @CreatedBy = '')";
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
query += " AND CreateDate BETWEEN @From AND @To";
}
}
if (ddlTables.SelectedValue.ToLower() == "cards")
{
query = "SELECT Uid,image,CreatedBy,Role,CreatedDate FROM CardTbl WHERE (CreatedBy=@CreatedBy OR @CreatedBy = '')";
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
query += " AND CreatedDate BETWEEN @From AND @To";
}
}
if (ddlTables.SelectedValue.ToLower() == "certificates")
{
query = "SELECT Uid,image,CreatedBy,Role,CreatedDate FROM CertTbl WHERE (CreatedBy=@CreatedBy OR @CreatedBy = '')";
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
query += " AND CreatedDate BETWEEN @From AND @To";
}
}
if (ddlTables.SelectedValue.ToLower() == "reciepts")
{
query = "SELECT Uid,image,CreatedBy,Name,CreatedDate FROM tablereceipt WHERE (CreatedBy=@CreatedBy OR @CreatedBy = '')";
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
query += " AND CreatedDate BETWEEN @From AND @To";
}
}
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@CreatedBy", createby.Text);
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
cmd.Parameters.AddWithValue("@From", TextBox1.Text);
cmd.Parameters.AddWithValue("@To", Dat.Text);
}
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}