Jaya Prakash

Jaya Prakash

  • 548
  • 2.3k
  • 63.4k

Search With multiple Options in a TextBox

Feb 27 2023 10:23 AM

i can able to search with id and mobno

but i also want to search with name i unable to do it  when i search with name it showing conversion error bcus it is directly executing

this condition showing conversion error how can i achieve it..

private void BindGrid()
{
    int flag;
    SqlCommand cmd = new SqlCommand("sp_GridVS", con);
    cmd.CommandType = CommandType.StoredProcedure;
    if (TextBox3.Text != "")
    {
        cmd.Parameters.AddWithValue("@mobileno", Convert.ToInt64(TextBox3.Text));
        cmd.Parameters.AddWithValue("@id", Convert.ToInt64(TextBox3.Text));
        cmd.Parameters.AddWithValue("@flag", 1);
    }
    else if (TextBox3.Text == "")
    {
        cmd.Parameters.AddWithValue("@mobileno","");
        cmd.Parameters.AddWithValue("@id", 0);
        cmd.Parameters.AddWithValue("@flag", 0);
    }
    DataTable dt = new DataTable();
    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
    {
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

This  is my sp

CREATE procedure sp_GridVS  
@flag int,@id bigint,@mobileno bigint  
as begin  
if(@flag=1)  
begin  
select *  from gridview where mobileno=@mobileno or id=@id  
end  
if(@flag=0)  
begin  
select *  from gridview  
end  
end

This is for seach button

protected void Button2_Click(object sender, EventArgs e)
{
    if (TextBox3.Text == "")
    {
        Response.Write("<script>alert('pls enter phone number or id ')</script>");
        BindGrid();
    }
    BindGrid();
}

 


Answers (7)