简体   繁体   中英

Setting parameter for stored procedure

I have setup a stored procedure that uses one input parameter ( @QuestionID ) to retrieve a single record (record comes from elements of multiple data tables).

Have created a datareader and am populating values on my web page with these values.

What I don't know how to do is to set the value of the input parameter off of someone clicking the link to open the "details" page from a list (on the referring page).

Referring page where someone clicks the details record and the QuestionID is set:

<asp:GridView ID="GridView1" runat="server" Caption="Submitted Questions" AllowSorting="True" 
     CaptionAlign="Left" EmptyDataText="You have not submitted any Questions." PageSize="5"
     AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="150" />
        <asp:BoundField DataField="SubmitDate" HeaderText="Submitted Date" ItemStyle-Width="50" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="Details" runat="server" Text="View Details" PostBackUrl='<%# "~/Submit/Submit_Detail.aspx?Id=" + Eval("QuestionID")  %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="Clone" runat="server" Text="Create Clone" PostBackUrl='<%# "~/Submit/Submit_Clone.aspx?Id=" + Eval("QuestionID")  %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

DataReader control that is currently hardcoded to @QuestionID value of 1, I need to get this to accept the querystring value being sent from the referring page:

string sConstr = ConfigurationManager.ConnectionStrings["CS1"].ConnectionString;
SqlConnection Conn = new SqlConnection(sConstr);

using (Conn)
{
    SqlCommand command = new SqlCommand("QuestionDetail", Conn);
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.Add(new SqlParameter("@QuestionID", SqlDbType.BigInt));
    command.Parameters["@QuestionID"].Value = 1;

    Conn.Open();
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
        txt_author.Text = reader["Author"].ToString();

    reader.Close();
}

You should be able to read it from the Request object assuming it matches the URLs provided within your LinkButtons (ie the querystring parameter is "Id") :

command.Parameters["@QuestionID"].Value = Convert.ToInt32(Request["Id"]);

You would just need to adjust this accordingly if your parameter was something different.

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