简体   繁体   中英

How to populate gridview using parameter from stored procedure pivot

I am new to this can anyone help me fix this please, been trying lot but cant get through. I am trying to populate gridview using a stored procedure with a parameter using pivot, in my where clause when I assign value directly then it works fine but when I use parameter so that user can have choice then it throws an error. Thanks in advance.

My procedure :

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetMarksheetByExamId]
    (@SubjectId INT)
AS
    DECLARE @cols AS NVARCHAR(MAX),
            @query  AS NVARCHAR(MAX)

    SELECT @cols = STUFF((SELECT ',' + QUOTENAME(Subjects) 
                          FROM SMarksheet
                          GROUP BY Subjects
                          -- ORDER BY Id
                          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

    SET @query = 'SELECT SubjectId, StudentName ' + @cols + ' FROM 
             (SELECT SubjectId, StudentName, Subjects, TotalM 
              FROM SMarksheet  
              WHERE SubjectId = @SubjectId) x
             PIVOT
                 (MAX(TotalM)
                  FOR Subjects IN (' + @cols +')) p '

    EXEC sp_executesql @query;

My gridview:

<table border="1" width="60%" align="center">
    <tr>
        <td align="center">
            <asp:TextBox ID="txt_Id" runat="server" AutoPostBack="true">1</asp:TextBox>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
        </td>
    </tr>
    <tr>
        <td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
            </asp:GridView>
        </td>
    </tr>
</table>

Code behind:

protected void Button1_Click(object sender, EventArgs e)
{
    DataSet ds = new DataSet();

    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand command = new SqlCommand())
        {
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "GetMarksheetByExamId";
            command.Connection = con;

            //  command.Parameters.AddWithValue("@SubjectId", b);
            command.Parameters.Add(new SqlParameter("@SubjectId", txt_Id.Text));

            con.Open();

            SqlDataAdapter a = new SqlDataAdapter(command);
            a.Fill(ds);
        }
    }

    GridView1.DataSource = ds;
    GridView1.DataBind();
}

Looks like your Variable assignment is inside the dynamic query. You need to change @Query assignment as below:

set @query = 'SELECT SubjectId, StudentName ' + @cols + ' from 
         (
            select SubjectId, StudentName, Subjects, TotalM 
            from SMarksheet  
            where SubjectId=@SubId


        ) x
        pivot 
        (
            max(TotalM)
            for Subjects in (' + @cols +')
        ) p '

exec sp_executesql @query, @SubId = @SubjectId;

You can pass subjected as int parameter as above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM