Mary Bergeron

Mary Bergeron

  • NA
  • 10
  • 7.5k

Search box to call stored proc and return data to grid

Nov 9 2011 2:59 PM
I have a Windows form in Visual Studio 2010 with a grid and it is bound to a stored procedure.  What I need to add to my form is a text box that allows a user to enter a name or part of a name and then have the results display in the grid.  Below is the code that has been utilized thus far:


Here is the code for the stored proc:

CREATE PROCEDURE

dbo.Talisma_Search

(@Talisma_Search NVARCHAR(255))

AS

SELECT

[dbo].[tblTalisma_Lookup].[tName]

, [dbo].[tblTalisma_Lookup].[tSSN]

, [dbo].[tblTalisma_Lookup].[tEmail]

FROM

[dbo].[tblTalisma_Lookup]

WHERE

[dbo].[tblTalisma_Lookup].[tName] LIKE  '%'+@Talisma_Search + '%'

OR [dbo].[tblTalisma_Lookup].[tSSN] LIKE  '%'+@Talisma_Search + '%'

OR [dbo].[tblTalisma_Lookup].[tEmail] LIKE  '%'+@Talisma_Search + '%'

ORDER BY

[dbo].[tblTalisma_Lookup].[tName]

Here is the code to execute the stored proc but the name in single quotes will need to change each time:

EXEC dbo.talisma_Search @talisma_Search ='savage'

Here is the code  I have on the Visual Studio Windows Form right now:

        private void
LoadData(Int32 Talisma_Search)

        {

            SqlConnection conn = new
SqlConnection("Data
Source='talismadb';Initial Catalog=tlMain;Integrated Security=True"
);

            // change this connection string according to you settings
of sql server

            SqlCommand cmd = new SqlCommand("dbo.Talisma_Search",

conn);

           
cmd.CommandText = "Talisma_Search";
         
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Talisma_Search
"
, Talisma_Search);

            SqlDataAdapter adapter = new
SqlDataAdapter(cmd);

            DataSet dataSet1 = new
DataSet();
          
dataSet1.Tables.Clear();
          
adapter.Fill(dataSet1, "Table");



Answers (2)