Per popolare una textbox con dati provenienti da db, sto usando una procedura che ritengo un tantino grezza che è questa:
Default.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string Scn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("clienti.mdb");
OleDbConnection cnn = new OleDbConnection(Scn);
cnn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Categories", cnn);
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
TextBox1.Text = rdr["Generi"].ToString();
DataGrid1.DataSource = rdr;
DataGrid1.DataBind();
}
}
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Pagina senza titolo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#999999" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataGrid></div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
Questo metodo va bene oppure, come penso che dovrebbe essere, qualcuno di voi mi consiglia con il DataBinding linkandomi qualche tutorial online?