Click here to Skip to main content
16,005,339 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void btnSearch_Click(object sender, EventArgs e)
        {
String ConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
            SqlConnection con = new SqlConnection(ConnString);
           
            if (txtslipno.Text.Trim() != "")
            {
                SqlCommand cmd = new SqlCommand("select * from Sample where Slipno  = '" + textbox1.Text + "'", con);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
                con.Close();
            }

            if (txtzone.Text.Trim() != "")
            {
                SqlCommand cmd = new SqlCommand("select * from Sample where Zone  = '" + textbox2.Text + "'", con1);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
                con.Close();


            }
            if (txtsetion.Text.Trim() != "")
            {
                SqlCommand cmd = new SqlCommand("select * from Sample where Section  = '" + textbox3.Text + "'", con2);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
                con.Close();
            }
        }


from the above three text box is there.

Slip no textbox1 Zone textbox2 Section textbox3


i want to validate from the above, if textbox1 slip no entered value is not in database shows the message to use Records not found


similarly if textbox2 zone entered value is not in database shows message to user records not found

similarly if textbox3 section entered value is not in database shows message to user records not found


for that how to do in my above code.

What I have tried:

Refer to describe section.
Posted
Updated 8-Jul-18 15:37pm
v2
Comments
Bryian Tan 8-Jul-18 16:14pm    
is this a Windows application? and are user allow to enter value into the three textboxes then submit or one textbox then submit?
David Crow 8-Jul-18 22:00pm    
Shouldn't this:

if (txtslipno.Text.Trim() != "")
{
    SqlCommand cmd = new SqlCommand("select * from Sample where Slipno  = '" + textbox1.Text + "'", con);


be this instead:

if (txtslipno.Text.Trim() != "")
{
    SqlCommand cmd = new SqlCommand("select * from Sample where Slipno  = '" + txtslipno.Text + "'", con);

C#
SqlCommand cmd = new SqlCommand("select * from Sample where Slipno = '" + textbox1.Text + "'", con);

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
As mentioned by ppolymorphe your code has an issue with SQL injection and you need to update code for that one.

Considering your question you can try something below - if you have any matching record you will receive it back with database call otherwise you will simply return nothing, so check the size of records you get back as below.


C#
if (txtslipno.Text.Trim() != "")
{
    SqlCommand cmd = new SqlCommand("select * from Sample where Slipno  = '" + textbox1.Text + "'", con);
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.SelectCommand = cmd;
    da.Fill(ds);

    if(ds.Tables != null && ds.Tables[0].Rows.Count > 0)
    {
       //you got DS populated with your slip number  
        grdRpt.DataSource = ds;
        grdRpt.DataBind();
    }
    else
    { 
        //no record found for your slip number 
    }
    con.Close();
}
 
Share this answer
 

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

  Print Answers RSS


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