Archivo Julio, 2009

Cómo pasar el nombre del usuario logueado como parámetro a un ObjectDataSource

Los ObjectDataSource soportan por default diferentes tipos de parámetros, como controles, cookies, Form, QueryString, Profile y Session. Sin embargo si quieres pasar como parámetro el UserName del usuario logueado no lo puedes hacer con ninguno de los tipos antes mencionados.

Para hacerlo debes utilizar el evento Selecting del ObjectDataSource.

Esto funciona así, aquí tenemos el ObjectDataSource:

ASP:
  1. <asp:ObjectDataSource ID="ODSEjemplo" runat="server"
  2.         OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
  3.         TypeName="BLLObtenerDatos">
  4.     </asp:ObjectDataSource>

Fíjate como no hemos incluido ningún parámetro. Éste lo generamos en el code-behind:

VB.NET:
  1. Protected Sub ODSEjemplo_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles ODSEjemplo.Selecting
  2.         e.InputParameters("MiParametro") = User.Identity.Name
  3.     End Sub

Nótese que en el ejemplo, el método GetData recibe un sólo parámetro de tipo String, mismo que coincide con el tipo de User.Identity.Name

Así de sencillo.

No hay Comentarios

Cómo leer un ConnectionString del web.config mediante código

En nuestro web.config guardamos los ConnectionStrings de nuestras bases de datos.

Ejemplo:

ASP:
  1. <connectionStrings>
  2.     <add name="MiConnectionString" connectionString="Data Source=Servidor\INSTANCIASQL;Initial Catalog=MiBaseDeDatos;Persist Security Info=True;User ID=NombreDeusuario;Password=xxxxxxxxxx" providerName="System.Data.SqlClient" />
  3.   </connectionStrings>

Es posible leer estos valores desde code-behind, de la siguiente forma:

VB.NET:
  1. Dim SuperSecreto As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("MiConnectionString").ConnectionString

1 Comentario