How to retrieve the values from a SP

Posted on 2006-05-23
Last Modified: 2010-04-23
Hi Experts,

I have a simple Sp that I want to fill a datagridview with, but unfortunately I'm not to good with SP myself.

Here is the SP:

CREATE PROCEDURE [ichanneluser].[test3]

@BrukerID nvarchar(50)

SELECT     jpst_Main.ID, jpst_Main.Heading, jpst_Main.Body, jpst_Main.OpprettetAv, jpst_Main.OpprettetDato, jpst_Main.Rute, jpst_Main.Avdeling,
FROM         jpst_Main INNER JOIN
                      jpst_Detail ON jpst_Main.ID = jpst_Detail.ID
WHERE     (jpst_Detail.BrukerID = @BrukerID)

Here is the vb code

                Dim sqlConn As New SqlClient.SqlConnection
                sqlConn.ConnectionString = getConnection2()

It is here I would like the datagridview called "dg1_1" to be filled vith the results from the SP.

Question by:jonastovsen
    LVL 6

    Expert Comment


    first create a sql command with type as stored proc and the textas the SP's name

    then create a dataadpter and assign the commad as the selectcommand for the dataadpter(and need not to open the connection expicitly)

    and then create a dataset and then fill the dataset with dataadpters fill command

    and assing that ds as datasourse for datagridview and call datagridview's databind method


    LVL 6

    Accepted Solution


    private SqlConnection conn = new SqlConnection();
    private SqlCommand cmd = new SqlCommand();
    private SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds;

    conn.ConnectionString = "ConnectionString";
    cmd.CommandText = "Sp name";
    cmd.CommandType = StoredProcedure;
    cmd.Connection = conn;
    ------------add the parameter to the command-------imp step
    SqlParameter sSearchString = new SqlParameter();
                            sSearchString.ParameterName = "@BrukerID";
                            sSearchString.Value = txtSearchString.Text;----source of value
                            sSearchString.SqlDbType = SqlDbType.VarChar;
                            sSearchString.Size = 1000;

    da.SelectCommand = cmd;
    dgSearchResult.DataSource = ds;



    Author Comment


    Thank you SandeepRR.

    You just saved my day!

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    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…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    731 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

    16 Experts available now in Live!

    Get 1:1 Help Now