Hi pratikshir,
As per your query i have create the sample. If you have any doubt please revert me back. Here i am making use of TOP cluase to show you sample. So you need to remove the TOP from your select query.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
Download and install Northwind Database
SQL
CREATE PROCEDURE CustomerSearch
@ContactName NVARCHAR(500) = NULL
,@Phone NVARCHAR(500) = NULL
AS
BEGIN
SELECT TOP 10 CustomerID,ContactName,City,Phone
FROM Customers
WHERE (ContactName = @ContactName OR @ContactName IS NULL)
AND (Phone = @Phone OR @Phone IS NULL)
END
Execute Query
EXEC CustomerSearch 'Mudassar Khan','(26) 642-7012'
Output
CustomerID |
ContactName |
City |
Phone |
AAFKM |
Mudassar Khan |
Warszawa |
(26) 642-7012 |
Execute Query
EXEC CustomerSearch null,'(26) 642-7012'
Output
CustomerID |
ContactName |
City |
Phone |
AAFKM |
Mudassar Khan |
Warszawa |
(26) 642-7012 |
WOLA |
Zbyszek's Piestrzeniewicz |
Warszawa |
(26) 642-7012 |
WOLAB |
Zbyszek's |
Warszawa |
(26) 642-7012 |
WOLZA |
Zbyszek Piestrzeniewicz |
Warszawa |
(26) 642-7012 |
Execute Query
EXEC CustomerSearch 'Mudassar Khan',null
Output
CustomerID |
ContactName |
City |
Phone |
AAFKM |
Mudassar Khan |
Warszawa |
(26) 642-7012 |
Execute Query
EXEC CustomerSearch null,null
Output
CustomerID |
ContactName |
City |
Phone |
AAFKM |
Mudassar Khan |
Warszawa |
(26) 642-7012 |
ALFKI |
Maria |
Boise |
030-0074321 |
ANATR |
Ana Trujillo |
México D.F. |
(5) 555-4729 |
ANTON |
Antonio Moreno |
Montréal |
(5) 555-3932 |
AROUT |
Thomas Hardy |
Mannheim |
(171) 555-7788 |
BERGS |
Christina Berglund |
Luleå |
0921-12 34 65 |
BLAUS |
Hanna Moos |
Mannheim |
0621-08460 |
BLONP |
Frédérique Citeaux |
Strasbourg |
88.60.15.31 |
BOLID |
Martín Sommer |
Madrid |
(91) 555 22 82 |
BONAP |
Laurence Lebihan |
Marseille |
91.24.45.40 |
HTML
<table>
<tr>
<td>
Enter Mobile No:
</td>
<td>
<asp:TextBox ID="txtMobileNo" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Enter Name :
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="gvDetails" runat="server">
</asp:GridView>
C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
string cns = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cns))
{
SqlCommand cmd = new SqlCommand("CustomersSearch");
if (!string.IsNullOrEmpty(txtMobileNo.Text))
{
cmd.Parameters.AddWithValue("@Phone", txtMobileNo.Text);
}
if (!string.IsNullOrEmpty(txtName.Text))
{
cmd.Parameters.AddWithValue("@ContactName", txtName.Text);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
gvDetails.DataSource = cmd.ExecuteReader();
gvDetails.DataBind();
con.Close();
}
}
VB.Net
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)
Dim cns As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Using con As New SqlConnection(cns)
Dim cmd As New SqlCommand("CustomersSearch")
If Not String.IsNullOrEmpty(txtMobileNo.Text) Then
cmd.Parameters.AddWithValue("@Phone", txtMobileNo.Text)
End If
If Not String.IsNullOrEmpty(txtName.Text) Then
cmd.Parameters.AddWithValue("@ContactName", txtName.Text)
End If
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
con.Open()
gvDetails.DataSource = cmd.ExecuteReader()
gvDetails.DataBind()
con.Close()
End Using
End Sub
Screenshot
