Asp. NET and database related skills-Basic application

Source: Internet
Author: User
first, get the value of data table I row J column

To establish and open a database connection
OleDbConnection conn=new OleDbConnection ();
Conn. Connectionstring=strconnectionstring;//strconnectionstring a 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 ()//data Table I row J column value
Conn.close ();


Ii. read the data into the DropDownList

(1) Add data in DropDownList

To establish and open a database connection
OleDbConnection conn=new OleDbConnection ();
Conn. Connectionstring=strconnectionstring;//strconnectionstring a database connection string
Conn. Open ();

String Sql= "SELECT * from Newsclass ORDER by ClassId Desc";
Set up 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 value
This. Dropdownlist1.databind ();

Conn. Close ();

(2) Select one of the DropDownList

This. DropDownList1.Items.FindByValue (dr["ClassID"). ToString (). Trim ()). SELECTED=TRUE;//DR for DataRow

third, the classification of the code to retrieve the corresponding category name and display in the DataGrid

(1). Code in ASPX (ClassID is a classified encoding):

<asp:templatecolumn headertext= "category" >
<ItemTemplate>
<asp:label id=lblclass runat= "server" text= ' <%# getclassname (Convert.ToInt32 (DataBinder.Eval, " Dataitem.classid "))%> ' >
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

(2) C # code:

<summary>
The category column returns text based on the number
</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;//returns the classid corresponding classname
}
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.