0
我有一個表:如何通過在C#列表下拉到存儲過程在SQLSERVER的價值
和乘以最後的結果如下:
一程序的定義如下:(必須將下拉列表的值與數據庫中的值相乘,然後將它們水平相加,並在最終結果中顯示resault。
CREATE PROCEDURE [dbo].[Cstored]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Ta int
DECLARE @Em int
DECLARE @Ma int
DECLARE @Sa int
SET @Em = 8 >>>> need to be set by user selection in dropdows list
SET @Ta= 6 >>>> need to be set by user selection in dropdows list
SET @Sa= 5 >>>> need to be set by user selection in dropdows list
SET @Ma = 7 >>>> need to be set by user selection in dropdows list
SELECT
Namec,
Tar*@Ta AS val1,
Emk*@Em as val2,
Mas*@Ma as val3,
San*@Sa As val4,
(Tar*@Ta)+(Emk*@Em)+(Mas*@Ma)+(San*@Sa) as finalresult
FROM Cdetail
END
正如你可以在屏幕截圖中看到的那樣,它在數據庫中運行良好,但是該值已在手動設置中。我希望它通過用戶選擇來設置。
SET @Em = 8
它是數據庫,但我創建了ASP網頁一些下拉列表,以便用戶如下選擇值:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Value="@Em">Emk</asp:ListItem>
<asp:ListItem Value="@Ta">Tar</asp:ListItem>
<asp:ListItem Value="@Sa">San</asp:ListItem>
<asp:ListItem Value="@Ma">Mas</asp:ListItem>
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="@Em">Emk</asp:ListItem>
<asp:ListItem Value="@Ta">Tar</asp:ListItem>
<asp:ListItem Value="@Sa">San</asp:ListItem>
<asp:ListItem Value="@Ma">Mas</asp:ListItem>
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="@Em">Emk</asp:ListItem>
<asp:ListItem Value="@Ta">Tar</asp:ListItem>
<asp:ListItem Value="@Sa">San</asp:ListItem>
<asp:ListItem Value="@Ma">Mas</asp:ListItem>
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownList4" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="@Em">Emk</asp:ListItem>
<asp:ListItem Value="@Ta">Tar</asp:ListItem>
<asp:ListItem Value="@Sa">San</asp:ListItem>
<asp:ListItem Value="@Ma">Mas</asp:ListItem>
</asp:DropDownList>
每個下拉菜單需要有一個默認值如下:
DropDownList1.SelectedValue = 8;
DropDownList2.SelectedValue = 7;
DropDownList3.SelectedValue = 6;
DropDownList4.SelectedValue = 5;
這意味着下拉1中選擇的值必須發送到存儲過程的數字8(DD2數字7等等),然後設置變量然後乘以相關的v在數據庫中。 ,之後從上到下顯示gridview
中finalresult
的值。
我試着用下面的代碼,但它不起作用,我不知道如何發送下拉值來存儲過程,並根據選擇提供每個項目。
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList1.SelectedValue = 8;
DropDownList2.SelectedValue = 7;
DropDownList3.SelectedValue = 6;
DropDownList4.SelectedValue = 5;
SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["carConnectionString"]);
sqlCon.Open();
SqlCommand scCommand = new SqlCommand("Carstored", sqlCon);
scCommand.CommandType = CommandType.StoredProcedure;
scCommand.Parameters.AddWithValue("????", DropDownList1);
scCommand.Parameters.AddWithValue("????", DropDownList2);
scCommand.Parameters.AddWithValue("????", DropDownList3);
scCommand.Parameters.AddWithValue("????", DropDownList4);
using (SqlDataAdapter sda = new SqlDataAdapter(scCommand))
{
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
我想,如果用戶在dropdow 1選擇Emk
,8
數相乘(X)至所有Emk
的值在表,並且如果用戶在dropdow 2選擇Tar
,7
數相乘(X),將值表中的所有Tar
。
它不工作,有錯誤在StoredProcedure中,下拉列表沒有默認值(DropDownList1.SelectedValue = 8;等) – Mom
什麼錯誤?您需要驗證用戶輸入... –
in'sda.Fill(dt);'我收到此錯誤(無法將參數值從字符串轉換爲Int32), 此外,我找不到默認下拉值的定義((DropDownList1.SelectedValue = 8; DropDownList2.SelectedValue = 7; DropDownList3.SelectedValue = 6; DropDownList4.SelectedValue = 5;)) – Mom