1. Obtain the value of column j in row I of the data table.
// Create and open a database connection
OleDbConnection conn = new OleDbConnection ();
Conn. ConnectionString = strConnectionString; // strConnectionString is the database connection string
Conn. Open ();
String SQL = "select * from NewsClass order by ClassId desc ";
String x;
DataSet ds = new DataSet ();
OleDbDataAdapter da = new OleDbDataAdapter (SQL, conn );
Da. Fill (ds, "NewsTable ");
DataTable dt = ds. Tables ["NewsTable"];
X = dt. Rows [I] [1]. ToString () // The value of column j in row I of the data table
Conn. close ();
Ii. Read data into DropDownList
(1) Add data to DropDownList
// Create and open a database connection
OleDbConnection conn = new OleDbConnection ();
Conn. ConnectionString = strConnectionString; // strConnectionString is the database connection string
Conn. Open ();
String SQL = "select * from NewsClass order by ClassId desc ";
// Create a dataset
DataSet ds = new DataSet ();
OleDbDataAdapter da = new OleDbDataAdapter (SQL, conn );
Da. Fill (ds, "NewsTable ");
This. DropDownList1.DataSource = ds;
This. DropDownList1.DataTextField = "ClassName"; // Text Value
This. DropDownList1.DataValueField = "ClassID"; // Value
This. DropDownList1.DataBind ();
Conn. Close ();
(2) Select an item in DropDownList.
This. DropDownList1.Items. FindByValue (dr ["ClassID"]. ToString (). Trim (). Selected = true; // dr is DataRow
3. Search for the corresponding category name and display it in the DataGrid
(1). Code in ASPX (ClassID is classification code ):
<Asp: TemplateColumn HeaderText = "class">
<ItemTemplate>
<Asp: Label id = lblClass runat = "server" Text = '<% # GetClassName (Convert. toInt32 (DataBinder. eval (Container, "DataItem. classID ") %> '>
</Asp: Label>
</ItemTemplate>
</Asp: TemplateColumn>
(2) C # code:
/// <Summary>
/// The "category" column returns text based on numbers
/// </Summary>
/// <Param name = "IsPassed"> </param>
/// <Returns> </returns>
Public string GetClassName (int ClassID)
{
OleDbConnection conn = new OleDbConnection ();
Conn. ConnectionString = strConnectionString;
Conn. Open ();
String SQL = "select * from NewsClass where ClassID =" + ClassID;
DataSet ds = new DataSet ();
OleDbDataAdapter da = new OleDbDataAdapter (SQL, conn );
Da. Fill (ds, "ClassTable ");
DataTable dt = ds. Tables ["ClassTable"];
String strClassName = dt. Rows [0] ["ClassName"]. ToString ();
Conn. Close ();
Return strClassName; // return the ClassName corresponding to the ClassID
}