The writing of data binding in asp.net

Source: Internet
Author: User
asp.net| data

Using System;
Using System.Data;
Using System.Data.SqlClient;
......
Namespace Xxxx.xxxxxxx
{
<summary>
A summary description of XXXXX.
</summary>
public class Xxxxx:System.Web.UI.Page
{
protected System.Data.DataRow Dr;
protected System.Data.SqlClient.SqlConnection Conn;
protected System.Data.SqlClient.SqlCommand mycmd,mysqlcmd;
private void Page_Load (object sender, System.EventArgs e)
{
String connstr= "Data source=xxx.xxx.xxx.xxx;uid=xxxx;pwd=****;d atabase=xxxx";;
This.conn=new System.Data.SqlClient.SqlConnection (CONNSTR);
}
Binding Data Using SqlDataReader
private void Xxxx_bound ()
{
Conn. Open ();
Sql= "SELECT * from XXXX";

MYCMD = conn. CreateCommand ();
Mycmd.commandtext =sql;
SqlDataReader Sqr=mycmd.executereader ();

while (SQR. Read ())
{
Tb_xxxx. Text=sqr[0]. ToString (). Trim ();
......

for (int i=0; i<ddl_xxxx. Items.Count; i++)
{
if (ddl_xxxx. Items[i]. Value.trim () = = Sqr[8]. ToString (). Trim ())
{
Ddl_xxxx. SelectedIndex = i;
}
}

&NBSP;&NBSP;&NBSP;&NBSP;
   }
   conn. Close ();
  }
//Binding after data is accessed using DataTable
  private void Datagrid2_bound ()
  {
    Conn. Open ();
   sqlcommand mycommand= Conn. CreateCommand ();

   mycommand.commandtext= "sp_xxxxxx";
   myCommand.CommandType=CommandType.StoredProcedure;
   sqlparameter para_type = MyCommand.Parameters.Add ("@sql", SqlDbType.VarChar);
   para_type.value = "";
   sqldatareader sqldr1 = Mycommand.executereader ();
   datatable dt=new DataTable ();
   dt. Columns.Add (New DataColumn ("XXXX", typeof (String));
    .....
   while (SQLDR1. Read ())
   {    
    dr = dt. NewRow ();
    for (int i=0; i<sqldr1. FieldCount; i++)
    {     
     dr[i] = sqldr1[ I]. ToString ();
    }
    dt. Rows.Add (DR);
   }

DataView Source = new DataView (DT);
Datagrid2.datasource=source;
L_count. text= "Total" +source.count.tostring () + "record";

Datagrid2.databind ();
Conn. Close ();

}
Binding Data using a dataset
public void Bindgrid (String SortField)
{
String SqlString = "Select User name, name from Ryxx";
Conn. Open ();
SqlDataAdapter mycommand = new SqlDataAdapter (SqlString, Conn);
DataSet ds = new DataSet ();
Mycommand.fill (ds, "Ryxx");
DataView Source = ds. tables["Ryxx"]. DefaultView;
Source.sort = SortField;
Datagrid1.datasource=source;
Datagrid1.databind ();
Conn. Close ();
}
Execute an SQL statement other than select
private void Ddl_xxxx_bind ()
{
String Updateshry;
updateshry= "Update ryxx set user name = ' Fdsas ', name = ' KJDSJ '";
SqlCommand comshry=new SqlCommand (updateshry,conn);
Conn. Open ();
Try
{
Cm.executenonquery ();
}
catch (SqlException E)
{
This. Response.Write ("<script language=javascript>alert (' Anomaly Information:" +e.tostring () + "');</script>");
}
Conn. Close ();
}
}
}



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.