Hello,
Why not construct the SQL dynamically. If a value is not specified in textbox then you may not want to include that in where condition of your sql. Formulate your query as shown below.
Hello,
Why not construct the SQL dynamically. If a value is not specified in textbox then you may not want to include that in where condition of your sql. Formulate your query as shown below.
<pre lang="C#">StringBuilder sb = new StringBuilder("SELECT * FROM beneficiarydetails ", 1024);
int len - sb.Length;
if (!String.IsNullOrEmpty(textBox1.Text))
{
sb.append(" WHERE AccountNo = ");
.append(textBox1.Text)
.append("'";
}
if (!String.IsNullOrEmpty(textBox2.Text))
{
if (sb.length == len)
{
sb.append(" WHERE );
}
else
{
sb.append(" AND ");
}
sb.append(" empname = '")
.append(textBox2.Text.Trim())
.append("'");
}
if (!String.IsNullOrEmpty(textBox3.Text))
{
if (sb.length == len)
{
sb.append(" WHERE );
}
else
{
sb.append(" AND ");
}
sb.append(" ddocode = '")
.append(textBox3.Text)
.append("'");
}
Note:I will recommend to use SQLCommand with parameters to avoid SQL Injection.