Usage of asp.net DataGrid binding database instance

Source: Internet
Author: User

The DataGrid control is a multifunctional, multiple-column, data-bound grid. To customize the layout of the columns in the DataGrid, you can set the column type to template, and then modify the column's template. The DataGrid control does not require a template for rendering, which makes the control an ideal control for reporting scenarios. The DataGrid also supports selecting, editing, deleting, paging, and sorting according to columns and by button columns.


Open Database Tutorial Connection
Con. Open ();
SQL statement
SqlDataAdapter Da=new SqlDataAdapter ("Select Id,name from verify", con);
Recordset object
DataSet ds=new DataSet ();
Executes the query and loads the query results into the Recordset object and specifies an alias that can be referenced
Da. Fill (ds, "verify");
Add a Table control to the form, create three columns (ordinal, id,name) of the form (Id,name) as the data-bound column
Specifies that the data source for the table is a DataTable in the dataset dataset
Datagrid.datasource=ds. tables["Verify"];
Binding Data
Datagrid.databind ();

Instance

<%@ Page language= "VB"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.OleDb"%>

<script runat= "Server" >
' Declare connection
Dim Conn As New OleDbConnection (_
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "DATA source=" _
& Server.MapPath ("Employeedatabase.mdb;")


Sub GetData (Sender as Object, e as EventArgs)
Dim objcmd As OleDbCommand = new OleDbCommand _
("SELECT * FROM employee where ID = @ID", Conn)
Dim objreader As OleDbDataReader

Dim objparam As OleDbParameter
Objparam = ObjCmd.Parameters.Add ("@ID", OleDbType.Integer)
Objparam.direction = ParameterDirection.Input
Objparam.value = Tbid.text

Try
ObjCmd.Connection.Open ()
objreader = Objcmd.executereader

Dgdata.datasource = objreader
Dgdata.databind ()

Objreader.close
ObjCmd.Connection.Close ()
Catch Objex as FormatException
Lblmessage.text = Objex.message
Catch Objex as OleDbException
Lblmessage.text = "Database error!"
Catch Objex as Exception
Lblmessage.text = "Unknown error!"
End Try
End Sub
</script>

<form runat= "Server" >
<asp Tutorial: Label id= "lblmessage" runat= "Server"
Maintainstate=false/><br>
Enter an ID: <asp:textbox id= "Tbid" runat= "Server"
Autopostback=true
Ontextchanged=getdata/>
<asp:datagrid id= "Dgdata" runat= "Server"
Bordercolor= "BLACK"
Gridlines= "Vertical"
Width= "100%"
Font-name= "Arial"
Font-size= "8pt"
Headerstyle-backcolor= "#cccc99"
Itemstyle-backcolor= "#ffffff"
Alternatingitemstyle-backcolor= "#cccccc"
Autogeneratecolumns= "true"/>

</form>
</body>

Note:

accessing data
This section describes how to access data in a database and how to bind the data you access to a list control. You can use a dataset or DataReader class to get data from a database.
DataSet class
The dataset contains a complete representation of the data, including the table structure, the relationships between the tables, and the ordering of the data. The DataSet class is flexible enough to store any kind of information in a database in an Extended Markup Language (XML) file. The DataSet class is stateless; that is, you can pass these classes from the client to the server without connecting to the server connection resource. The following code demonstrates how to use a dataset to bind data to a control:

Note: You must modify the parameters of the connection string based on the needs of your environment.

Visual Basic. NET

Dim CNN As SqlConnection = New SqlConnection ("server= (local);" & _
"Database=pubs;integrated Security=sspi")
Dim cmd as SqlDataAdapter = New SqlDataAdapter ("select * from Authors", CNN)
Dim ds As DataSet = New DataSet ()
Cmd. Fill (DS)
MyRepeater.DataSource = ds
Myrepeater.databind ()

Visual C #. NET

SqlConnection cnn = new SqlConnection ("server=" (local);
                                         database=pubs;integrated Security=sspi ");
SqlDataAdapter da = new SqlDataAdapter ("select * from Authors", CNN); The
DataSet ds = new DataSet ();
da. Fill (DS);
MyRepeater.DataSource = ds;
Myrepeater.databind ();

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.