¿Cómo pasar el elemento seleccionado del menú desplegable de asp.net como entrada a un procedimiento almacenado en 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;


        }

preguntado el 28 de agosto de 12 a las 10:08

Your code doesn't make too much sense here. Where is your dropdown item? And Where is UserID definido? -

4 Respuestas

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); 

Respondido 28 ago 12, 10:08

sqlcmd.Parameters.AddWithValue("@event_id", yourDropDown.SelectedValue);

Respondido 28 ago 12, 10:08

Call your function like

string eventId = ddlEvents.SelectedValue;
GetDaysForEvents(eventId);

Respondido 28 ago 12, 10:08

-Usuario using statement to close your connection and dispose your unused resources.

-La DataAdapter open the connection by itself, so no need of 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;
}

Y en otro lugar

var events = yourClassInstance.GetDaysForEvents(dropDown.SelectedValue);

Respondido 28 ago 12, 10:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.