Foros del Web » Programación para mayores de 30 ;) » .NET »

Busqueda con la Opcion de un dropdownlist y un Boton exportando los datos a un Datagr

Estas en el tema de Busqueda con la Opcion de un dropdownlist y un Boton exportando los datos a un Datagr en el foro de .NET en Foros del Web. Quiero que seleccionando desde un Dropdown seleccione la opcion y esta se coloque en el Store Procedure y a su vez que con el boton ...
  #1 (permalink)  
Antiguo 03/03/2005, 16:14
 
Fecha de Ingreso: diciembre-2004
Ubicación: Caracas
Mensajes: 10
Antigüedad: 19 años, 5 meses
Puntos: 0
Exclamación Busqueda con la Opcion de un dropdownlist y un Boton exportando los datos a un Datagr

Quiero que seleccionando desde un Dropdown seleccione la opcion y esta se coloque en el Store Procedure y a su vez que con el boton despliegue la Respuesta en un datagrid. NO ME PASA DEL PRIMER CAMPO EN LA DROPDOWNLIST. HELP ME

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Director
{
/// <summary>
/// Descripción breve de bagencia.
/// </summary>
///
public class bagencia : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid GrdAuthors;
protected System.Web.UI.WebControls.Label lblRowCount;
protected System.Web.UI.WebControls.Button bntGetAuthors;
protected System.Web.UI.WebControls.DropDownList DropDownList1;

protected System.Web.UI.WebControls.Label Label1;
public string agencia;
private void Page_Load(object sender, System.EventArgs e)
{

if (!Page.IsPostBack)
{
this.LlenarConSQL();
}

this.bntGetAuthors.Click += new System.EventHandler(this.bntGetAuthors_Click);
}

private void LlenarConSQL()
{
//Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind
// Establece el string de conexión
string cadenaConexion = "Data Source=MSCCS000int01;" +
"uid=sa;pwd=;Initial Catalog=dbintracanarias;";

// Establece la consulta SQL a ejecutar
string consulta = "SELECT COD_UNI_ADM,DESCRIPCION_UNIDAD AS name FROM INTCAN_TB_UNIDAD_ADM ORDER BY DESCRIPCION_UNIDAD";

// Crea un DataAdapter que sera el encargado de ejecutar la consulta
// y Posteriormente ingresar los datos a un DataSet
SqlDataAdapter daAutores = new SqlDataAdapter(consulta, cadenaConexion);

// Crea el DataSet
DataSet dsAutores = new DataSet();

// Llena el DataSet con la información de la base de datos
daAutores.Fill(dsAutores,"Authors");

// Asigna el Datatable Autores como fuente de datos para el DropDownList
DropDownList1.DataSource = dsAutores.Tables["Authors"].DefaultView;

// Asigno el valor a mostrar en el DropDownList
DropDownList1.DataTextField = "name";

// Asigno el valor del value en el DropDownList
DropDownList1.DataValueField = "COD_UNI_ADM";
//agencia= DropDownList1.SelectedItem.Value;
// Llena el DropDownList con los datos
DropDownList1.DataBind();
}
#region Código generado por el Diseñador de Web Forms
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: llamada requerida por el Diseñador de Web Forms ASP.NET.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Método necesario para admitir el Diseñador. No se puede modificar
/// el contenido del método con el editor de código.
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedInd exChanged);
this.bntGetAuthors.Click += new System.EventHandler(this.bntGetAuthors_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


private void bntGetAuthors_Click(object sender, System.EventArgs e)
{
//Create a connection to the SQL Server; modify the connection string for your environment.
//SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;Truste d_Connection=yes");

SqlConnection MyConnection = new SqlConnection("server=msccs000int01; database=dbintracanarias; UID=sa;PWD='';");

//Create a DataAdapter, and then provide the name of the stored procedure.
SqlDataAdapter MyDataAdapter = new SqlDataAdapter("INTCAN_SP_SEL_AGENCIA", MyConnection);

//Set the command type as StoredProcedure.
MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

//Create and add a parameter to Parameters collection for the stored procedure.
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@AGENCIA", SqlDbType.VarChar, 40));
//Asignar los valores seleccionados

MyDataAdapter.SelectCommand.Parameters["@AGENCIA"].Value = DropDownList1.SelectedItem.Value;

//Create and add an output parameter to the Parameters collection.
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@RowCount", SqlDbType.Int, 20));

//Set the direction for the parameter. This parameter returns the Rows that are returned.
MyDataAdapter.SelectCommand.Parameters["@RowCount"].Direction = ParameterDirection.Output;

//Create a new DataSet to hold the records.
DataSet DS = new DataSet();

//Fill the DataSet with the rows that are returned.
MyDataAdapter.Fill(DS, "AuthorsByLastName");

//Get the number of rows returned, and assign it to the Label control.
//lblRowCount.Text = DS.Tables(0).Rows.Count().ToString() & " Rows Found!"
lblRowCount.Text = MyDataAdapter.SelectCommand.Parameters[1].Value + " Personas Encontradas ";

//Set the data source for the DataGrid as the DataSet that holds the rows.
GrdAuthors.DataSource = DS.Tables["AuthorsByLastName"].DefaultView;


GrdAuthors.DataBind();
MyDataAdapter.Dispose();
MyConnection.Close();
}

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
LlenarConSQL();
}
}
}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:13.