Solved

How do you get a grid to display the results of a stored procedure?

Posted on 2008-06-26
3
176 Views
Last Modified: 2013-12-16
I am trying to perform a simple lookup on a table and return the results in a webgrid.  I am getting no errors but the webgrid shows nothing after the postback.  I have attached a Microsoft SQL Data source to the webgrid and I have told my Microsoft SQL Data Source to use a stored procedure.  I then pass the stored procedure the data through @variables.  Can someone tell me what I am missing.  If I use the "Test Query" in the Microsoft SQL Datasource, I get data so I know there should be results returned.  Any help is greatly appreciated.  Here is the code and stored procedure I am using.

C# Code:
protected void btnSearchCarrierId_Click(object sender, Infragistics.WebUI.WebDataInput.ButtonEventArgs e)
    {
       
        SqlConnection InsLookUp;
        InsLookUp = new SqlConnection();
        InsLookUp.ConnectionString = ConfigurationManager.ConnectionStrings["123123"].ConnectionString;

        SqlCommand cmd;

        InsLookUp.Open();

        cmd = new SqlCommand("InsCompLookup", InsLookUp);
        cmd.CommandType = CommandType.StoredProcedure;

       
        // Input Parameters
        cmd.Parameters.Add("@CARRIER_NAME", SqlDbType.VarChar, 36).Value = tbCarrierName.Text;
        cmd.Parameters.Add("@CARRIER_ID", SqlDbType.VarChar, 15).Value = tbCarrierId.Text;
        cmd.Parameters.Add("@CITY", SqlDbType.VarChar, 20).Value = tbCarrierCity.Text;
        cmd.Parameters.Add("@STATE", SqlDbType.VarChar, 2).Value = tbCarrierState.Text;


        cmd.Parameters.Add("@ZIP", SqlDbType.VarChar, 9).Value = tbCarrierZip.Text;
        cmd.Parameters.Add("@PHONE", SqlDbType.VarChar, 11).Value = tbCarrierPhone.Text;


        //    execute the command and read the current Referral ID
        cmd.ExecuteNonQuery();
        this.UltraWebGrid3.DataBind();

        if (InsLookUp != null)
        {
            InsLookUp.Close();
        }


    }




SQL Stored Procedure:
USE [Chart Relay]
GO
/****** Object:  StoredProcedure [dbo].[InsCompLookup]    Script Date: 06/25/2008 09:16:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[InsCompLookup]

(
@CARRIER_NAME VARCHAR (36),
@CARRIER_ID VARCHAR (15),
@CITY VARCHAR (20),
@STATE VARCHAR (2),
@ZIP VARCHAR (9),
@PHONE VARCHAR (11)
)

AS
SET NOCOUNT ON

SELECT     INSURANCE_NPI, NAME, CITY, STATE, ZIP, PHONE
FROM         INSURANCE_COMPANY
WHERE     (INSURANCE_COMPANY.INSURANCE_NPI = @CARRIER_ID OR INSURANCE_COMPANY.NAME = @CARRIER_NAME OR INSURANCE_COMPANY.CITY = @CITY OR INSURANCE_COMPANY.STATE = @STATE OR INSURANCE_COMPANY.ZIP = @ZIP)

0
Comment
Question by:kwh3856
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi kwh3856;

Your most likely cause of your problem is these lines.

        //    execute the command and read the current Referral ID
        cmd.ExecuteNonQuery();

The, cmd.ExecuteNonQuery(), command just returns the number of rows effected by the query, which you are not even using, no data is returned.

Try doing this:

DataSet ds = new DataSet();    // You may want to place this at class level, access from other areas
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "InsCompLookup");   // The second parameter is just a locally defined table name to reference the table

Make sure to set the data source of the grid to ds.Table["InsCompLookup"] and remove the following lines of code.

  InsLookUp.Open();

  if (InsLookUp != null)
  {
      InsLookUp.Close();
  }

And replace this, cmd.ExecuteNonQuery();, with the above 3 lines of code. The SqlDataAdapter will take care of opening and closing the connection automatically.

Fernando
0
 

Author Closing Comment

by:kwh3856
Comment Utility
YOUR AWESOME !!!!!!!!!!!!

That was EXACTLY the answer I was looking for.  Many Many thanks!!!!

Thanks
Kenny
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem Kenny, glad I was able to help. ;=)
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now