[英]How to pass the asp.net dropdown selected item as an input to a stored procedure in c#?
This is the code i am using.. I want to pass the dropdown selected item as a input to the stored procedure and execute that.. how could i achieve that? 这是我正在使用的代码。我想将下拉选择项作为存储过程的输入并执行该操作。我如何实现这一目标?
public class DaysForEvents
{
public DataSet GetDaysForEvents(//This should be my input to stored procedure (i.e) dropdownselected value)
{
SqlConnection con = new SqlConnection(CommonSettings.GetEventsConnectionString());
try
{
SqlCommand sqlcmd = new SqlCommand("proc_fetch_event_days", con);
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
//DateFilter = 0;
//StatusFilter = 0;
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@event_id", Event_id);
// sqlcmd.Parameters["@UserID"].Value = UserID;
con.Open();
sqlda.Fill(ds);
con.Close();
return ds;
}
As I understand, you want this. 据我了解,您想要这个。
//get dropdown selected value and store in a variable
var eventId = dropdown.SelectedValue;
//pass this variable in the GetDaysForEvents method to get DataSet.
var dataSet = GetDaysForEvents(eventId);
sqlcmd.Parameters.AddWithValue("@event_id", yourDropDown.SelectedValue);
Call your function like 像这样调用您的函数
string eventId = ddlEvents.SelectedValue;
GetDaysForEvents(eventId);
-User using
statement to close your connection and dispose your unused resources. -用户
using
语句关闭您的连接并处置您未使用的资源。
-The DataAdapter
open the connection by itself, so no need of Open()
; DataAdapter
自行打开连接,因此不需要Open()
;
public DataSet GetDaysForEvents(string eventId)
{
var ds = new DataSet();
using(var connection = new SqlConnection("ConnectionString"))
{
using(var cmd = new SqlCommand("storedProcedureName", connection){ CommandType = CommandType.StoredProcedure })
{
cmd.Parameters.AddWithValue("@EventId", eventId);
using(var adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(ds);
}
}
}
return ds;
}
And somewhere else 还有其他地方
var events = yourClassInstance.GetDaysForEvents(dropDown.SelectedValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.