Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My Query is-

SQL
Query="Select * From beneficiarydetails where AccountNo='" + Int64.Parse(textBox1.Text) + "' or empname='" + textBox2.Text.Trim() + "' or ddocode='" + Int32.Parse(textBox3.Text) + "' ";


I'm Using Three TextBoxes One For DDOCODE,Second For Account No.,Third For Empname

and Searching using Only One textbox Value at a time.

Suppose if i Enter something in Textbox1 then on keypress Event If Something is Written in Texbox2 and Textbox3 then i'm Removing Textboxes Value Using String.Empty

so here problem occuring Suppose when i Entered AccountNo in textbox1 then Clicking oN Search then its

Throwing Error "Input String Was not in Correct Format"

it is because DDOCode is blank and its Converting Blank Value to Int.

I want to search only one textbox's Values at a time.

Can anybody help me Thanks in Advance
Posted
Comments
[no name] 23-Mar-13 9:24am    
Using a parameterized query instead of this SQL injection attack waiting to happen might help you.
Surendra0x2 23-Mar-13 9:30am    
Sir,Its Windows Application not Web.
[no name] 23-Mar-13 9:39am    
What, exactly, does that have to do with anything?
Monster Maker 23-Mar-13 9:59am    
If i got your problem right,you want Int value to be O when the textbox is empty??

1 solution

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.
C#
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900