ASP. NET and database related skills

Source: Internet
Author: User

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
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.