[英]How to write a stored procedure whose input parameter is a selection from a drop down list & pass the result into a blank text box?
這是我的過程,我已在SQL Management Studio中成功執行了該過程,但無法使其在Visual Studio中運行。 當我在Visual Studio中運行它時,不會讓我選擇輸入參數,因此我想在用戶從應用程序下拉菜單中選擇的輸入參數中運行它。
步驟如下:
Create PROCEDURE [dbo].[GetLastCustomerID_CustomerTypeSelect]
(@CustomerType varchar(20)= Null)
-- Add the parameters for the stored procedure here
--@CustomerType varchar = 'Norcross'
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Select case @CustomerType
When 'Norcross'
Then (SELECT MAX(custnum)
from TSarCustomer
where dbo.ParseAlphaChars(CustNum) between 25000 and 79999 and OpenedDate >
dateadd(d,-120,GETDATE()))
When 'Gaffey'
Then(SELECT MAX(custnum)
from TSarCustomer
where (dbo.ParseAlphaChars(CustNum) between 80000 and 150000) and OpenedDate > dateadd(d,-120,GETDATE()))
When 'CranePro'
then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%K' and OpenedDate > '1/1/2012')
When 'CranePro'
then(SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%C' and OpenedDate > '1/1/2005')
When 'Internet'
Then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%W' and OpenedDate > '1/1/2012')
When 'M'
Then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%M' and OpenedDate > '1/1/2012')
end
end
這是我的下拉菜單代碼,該下拉菜單的選項是從表中提取的。
<tr>
<td class="auto-style3">Customer Type:</td>
<td colspan="1" class="auto-style4">
<asp:DropDownList ID="DropDownListCustomerType" AutoPostBack="true" runat="server" DataSourceID="SqlDataSourceCustomerType" DataTextField="CustomerType" DataValueField="CustomerType" Height="22px" Width="113px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCustomerType" runat="server" ConnectionString="<%$ ConnectionStrings:AceWOMConnectionString %>" SelectCommand="SELECT * FROM [CustomeryType]"></asp:SqlDataSource>
</td>
<td class="auto-style3" >
Assign Take-Stock#:</td>
<td class="auto-style3">
<asp:TextBox ID="TSCustomerNumber" runat="server">
</asp:TextBox>
</td>
</tr>
因此,我需要幫助弄清楚用戶選擇下拉菜單時如何運行存儲過程,從下拉菜單中進行選擇,將其作為輸入參數傳遞給存儲過程,然后將過程的結果發布到標識為“ TSCustomerNumber”的空白文本框
任何和所有幫助將不勝感激!
在下拉列表中使用onselectedindexchanged事件處理程序。
onselectedindexchanged="itemSelected"
然后在您的代碼后面創建一個方法來處理事件並獲取dropdownlist值作為參數,調用SP傳入參數,並在完成后更新文本框:
protected void itemSelected(object sender, EventArgs e)
{
var val = DropDownListCustomerType.SelectedItem.Value;
using (SqlConnection con = new SqlConnection(myDataConnection)) {
using (SqlCommand cmd = new SqlCommand("GetLastCustomerID_CustomerTypeSelect", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerType", SqlDbType.VarChar).Value = val;
con.Open();
TSCustomerNumber.Text = (String) cmd.ExecuteScalar();
}
}
}
那至少應該讓你接近...
string query = "select Fechacumplimiento from TTareasIngreso where descripcion='" + dprTarea.Text + "' and descripcion='" + lblagencia.Text + "' "; SqlCommand cmd = new SqlCommand(query, conn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { TxtFechaPago.Text = dr["Fechacumplimiento"].ToString(); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.