Click here to Skip to main content
14,299,406 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to search a database in ASP.NET using a text box ??

how to write a search query number and strings ??

This my queary

SELECT cusName,cusCode FROM tblCustomer WHERE (cusName LIKE 'A' + '%')

cusCode-int
cusName-nvarchar

i want to develop a search function broth cusCode and cusName using one textbox
Posted
Updated 31-Jul-19 11:42am
Comments
Maciej Los 15-Jul-13 5:52am
   
What have you done so far?
Where are you stuck?
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can try this trick,


int result;
if (int.TryParse(textbox1.text, out result))
{
     // hence the input text is a integer so search in via CusCode
    SELECT cusName,cusCode FROM tblCustomer WHERE (cusCode  LIKE ' textbox1.text ' + '%')
}
else
{
     // hence the input text is a String so search in via CusName
    SELECT cusName,cusCode FROM tblCustomer WHERE (cusName LIKE ' textbox1.text  ' + '%')
}



Hope it helps.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hello Try this, This will work 100%



protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
{

SqlConnection con = new SqlConnection(_connString);
SqlCommand cmdSearch = new SqlCommand("Select cusName,cusCode from tblCustomer where cusName='" + TxtSearch.Text + "' or cusCode ='" + TxtSearch.Text + "' ", con);

SqlDataAdapter da = new SqlDataAdapter(cmdSearch);
DataTable dt = new DataTable();

da.Fill(dt);
if (dt.Rows.Count == 0)
{

ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Sorry No Records Found with this Keyword.....');document.location.href="/KB/answers/SamePage.aspx";", true);

}
else
{
GridViewTesting.DataSource = dt;
GridViewTesting.DataBind();
}
}
   
Comments
NotPolitcallyCorrect 15-Jul-13 8:20am
   
"This will work 100%", if you mean it will work 100% of the time to allow SQL injection attacks, then sure.
Rate this:
Please Sign up or sign in to vote.

Solution 4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("data source=DESKTOP-4UE7G07;initial catalog=Arpit7799;integrated security=true");

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Visible = false;
        TextBox3.Visible = false;
        TextBox4.Visible = false;
        Label2.Visible = false;
        Label3.Visible = false;
        Label4.Visible = false;
       }
    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlCommand cmd = new SqlCommand("select*from Table_6 where id="+Convert.ToInt32(TextBox1.Text),con);
        SqlDataAdapter adtp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        adtp.Fill(ds,"dt");
        con.Open();
        if (ds.Tables[0].Rows.Count > 0) 
        {
            TextBox2.Text = ds.Tables[0].Rows[0][1].ToString();
            TextBox3.Text = ds.Tables[0].Rows[0][2].ToString();
            TextBox4.Text = ds.Tables[0].Rows[0][3].ToString();
        }
         
        else
        {
            Response.Write("<script>alert('data not found')</script>");
        }
        TextBox2.Visible = true;
        TextBox3.Visible = true;
        TextBox4.Visible = true;
        Label2.Visible = true;
        Label3.Visible = true;
        Label4.Visible = true;


        }
    }
   
Comments
Member 13756920 1-Apr-18 6:50am
   
It is working well
CHill60 2-Apr-18 17:01pm
   
Default control names, vulnerable to sql injection attacks, not disposing of objects properly, no error handling...all that and over 4 years late

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100