answersLogoWhite

0

//define SQLDB connection string

string strSQLconnection = "Data Source=dbServer;Initial Catalog=DBNAME;Integrated Security=True;uname=username;password=pwd";

//initialize sqlconnection from the connection string.

SqlConnection sqlConnection = new SqlConnection(strSQLconnection);

//initialize the query to be fired.

SqlCommand sqlCommand = new SqlCommand("select * from table1", sqlConnection);

//open sql connection

sqlConnection.Open();

//Fire the query and open the reader to store the results

SqlDataReader reader = sqlCommand.ExecuteReader();

//define the datsource for the gridview

GridView1.DataSource = reader;

//bind the gridview. this will populate the gridview

GridView1.DataBind();

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

What is data bound in aspnet?

it is a data that is bounded. Example of control that bound data is GridView and DetailsView.


What is gridview in aspnet?

Displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items.Namespace: System.Web.UI.WebControlsAssembly: System.Web (in System.Web.dll) Syntax Visual Basic (Declaration) _ _ _Public Class GridView _Inherits CompositeDataBoundControl _Implements IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler Visual Basic (Usage) Dim instance As GridViewC# [ControlValuePropertyAttribute("SelectedValue")][AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)][AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]public class GridView : CompositeDataBoundControl, IPostBackContainer,IPostBackEventHandler, ICallbackContainer, ICallbackEventHandlerVisual C++ [ControlValuePropertyAttribute(L"SelectedValue")][AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)][AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]public ref class GridView : public CompositeDataBoundControl,IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandlerJScript public class GridView extends CompositeDataBoundControl implements IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandlerASP.NET RemarksThe GridView control is used to display the values of a data source in a table. Each column represents a field, while each row represents a record. The GridView control supports the following features:Binding to data source controls, such as SqlDataSource.Built-in sort capabilities.Built-in update and delete capabilities.Built-in paging capabilities.Built-in row selection capabilities.Programmatic access to the GridView object model to dynamically set properties, handle events, and so on.Multiple key fields.Multiple data fields for the hyperlink columns.Customizable appearance through themes and styles.To learn about the other data-bound controls that are available in ASP.NET, see ASP.NET Data-Bound Web Server Controls Overview.Note:If you are familiar with the DataGrid control from the .NET Framework version 1.0, the GridView control is the successor to the DataGrid control.Column FieldsEach column in the GridView control is represented by a DataControlField object. By default, the AutoGenerateColumns property is set to true, which creates an AutoGeneratedField object for each field in the data source. Each field is then rendered as a column in the GridView control in the order that each field appears in the data source.You can also manually control which column fields appear in the GridView control by setting the AutoGenerateColumns property to false and then defining your own column field collection. Different column field types determine the behavior of the columns in the control. The following table lists the different column field types that can be used.Column field typeDescriptionBoundFieldDisplays the value of a field in a data source. This is the default column type of the GridView control.ButtonFieldDisplays a command button for each item in the GridView control. This enables you to create a column of custom button controls, such as the Add or the Remove button.CheckBoxFieldDisplays a check box for each item in the GridView control. This column field type is commonly used to display fields with a Boolean value.CommandFieldDisplays predefined command buttons to perform select, edit, or delete operations.HyperLinkFieldDisplays the value of a field in a data source as a hyperlink. This column field type enables you to bind a second field to the hyperlink's URL.ImageFieldDisplays an image for each item in the GridView control.TemplateFieldDisplays user-defined content for each item in the GridView control according to a specified template. This column field type enables you to create a custom column field.To define a column field collection declaratively, first add opening and closing tags between the opening and closing tags of the GridView control. Next, list the column fields that you want to include between the opening and closing tags. The columns specified are added to the Columns collection in the order listed. The Columns collection stores all the column fields in the control and enables you to programmatically manage the column fields in the GridView control.Explicitly declared column fields can be displayed in combination with automatically generated column fields. When both are used, explicitly declared column fields are rendered first, followed by the automatically generated column fields.Note:Automatically generated column fields are not added to the Columns collection.Binding to DataThe GridView control can be bound to a data source control (such as SqlDataSource, ObjectDataSource, and so on), as well as any data source that implements the System.Collections..::.IEnumerable interface (such as System.Data..::.DataView, System.Collections..::.ArrayList, or System.Collections..::.Hashtable). Use one of the following methods to bind the GridView control to the appropriate data source type:To bind to a data source control, set the DataSourceID property of the GridView control to the ID value of the data source control. The GridView control automatically binds to the specified data source control and can take advantage of the data source control's capabilities to perform sorting, updating, deleting, and paging. This is the preferred method to bind to data.To bind to a data source that implements the System.Collections..::.IEnumerable interface, programmatically set the DataSource property of the GridView control to the data source and then call the DataBind method. When using this method, the GridView control does not provide built-in sort, update, delete, and paging functionality. You need to provide this functionality by using the appropriate event.For more information about data binding, see Accessing Data with ASP.NET.Note:This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. Whenever possible, it is strongly recommended that values are HTML-encoded before they are displayed in this control (the BoundField class HTML-encodes values by default). ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Introduction to the Validation Controls.Data OperationsThe GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control. When the GridView control is bound to a data source control, the GridView control can take advantage of the data source control's capabilities and provide automatic sort, update, and delete functionality.Note:The GridView control can provide support for sorting, updating, and deleting with other types of data sources. However, you will need to provide an appropriate event handler with the implementation for these operations.Sorting allows the user to sort the items in the GridView control with respect to a specific column by clicking on the column's header. To enable sorting, set the AllowSorting property to true.The automatic updating, deleting, and selection functionalities are enabled when a button in a ButtonField or TemplateField column field, with a command name of "Edit", "Delete", and "Select", respectively, is clicked. The GridView control can automatically add a CommandField column field with an Edit, Delete, or Select button if the AutoGenerateEditButton, AutoGenerateDeleteButton, or AutoGenerateSelectButton property is set to true, respectively.Note:Inserting records into the data source is not directly supported by the GridView control. However, it is possible to insert records by using the GridView control in conjunction with the DetailsView or FormView control. For more information, see DetailsView or FormView, respectively.Instead of displaying all the records in the data source at the same time, the GridView control can automatically break the records up into pages. To enable paging, set the AllowPaging property to true.Note:The GridView control is re-created on postback based on the information that is stored in ViewState. If the GridView control includes a TemplateField or a CommandField with the CausesValidation property set to true, then the EnableViewState property must also be set to true to ensure that concurrent data operations, such as updates and deletes, apply to the appropriate row.Customizing the User InterfaceYou can customize the appearance of the GridView control by setting the style properties for the different parts of the control. The following table lists the different style properties.Style propertyDescriptionAlternatingRowStyleThe style settings for the alternating data rows in the GridView control. When this property is set, the data rows are displayed alternating between the RowStyle settings and the AlternatingRowStyle settings.EditRowStyleThe style settings for the row being edited in the GridView control.EmptyDataRowStyleThe style settings for the empty data row displayed in the GridView control when the data source does not contain any records.FooterStyleThe style settings for the footer row of the GridView control.HeaderStyleThe style settings for the header row of the GridView control.PagerStyleThe style settings for the pager row of the GridView control.RowStyleThe style settings for the data rows in the GridView control. When the AlternatingRowStyle property is also set, the data rows are displayed alternating between the RowStyle settings and the AlternatingRowStyle settings.SelectedRowStyleThe style settings for the selected row in the GridView control.You can also show or hide different parts of the control. The following table lists the properties that control which parts are shown or hidden.PropertyDescriptionShowFooterShows or hides the footer section of the GridView control.ShowHeaderShows or hides the header section of the GridView control.EventsThe GridView control provides several events that you can program against. This enables you to run a custom routine whenever an event occurs. The following table lists the events that are supported by the GridView control.EventDescriptionPageIndexChangedOccurs when one of the pager buttons is clicked, but after the GridView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different page in the control.PageIndexChangingOccurs when one of the pager buttons is clicked, but before the GridView control handles the paging operation. This event is often used to cancel the paging operation.RowCancelingEditOccurs when a row's Cancel button is clicked, but before the GridView control exits edit mode. This event is often used to stop the canceling operation.RowCommandOccurs when a button is clicked in the GridView control. This event is often used to perform a task when a button is clicked in the control.RowCreatedOccurs when a new row is created in the GridView control. This event is often used to modify the contents of a row when the row is created.RowDataBoundOccurs when a data row is bound to data in the GridView control. This event is often used to modify the contents of a row when the row is bound to data.RowDeletedOccurs when a row's Delete button is clicked, but after the GridView control deletes the record from the data source. This event is often used to check the results of the delete operation.RowDeletingOccurs when a row's Delete button is clicked, but before the GridView control deletes the record from the data source. This event is often used to cancel the deleting operation.RowEditingOccurs when a row's Edit button is clicked, but before the GridView control enters edit mode. This event is often used to cancel the editing operation.RowUpdatedOccurs when a row's Update button is clicked, but after the GridView control updates the row. This event is often used to check the results of the update operation.RowUpdatingOccurs when a row's Update button is clicked, but before the GridView control updates the row. This event is often used to cancel the updating operation.SelectedIndexChangedOccurs when a row's Select button is clicked, but after the GridView control handles the select operation. This event is often used to perform a task after a row is selected in the control.SelectedIndexChangingOccurs when a row's Select button is clicked, but before the GridView control handles the select operation. This event is often used to cancel the selection operation.SortedOccurs when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation. This event is commonly used to perform a task after the user clicks a hyperlink to sort a column.SortingOccurs when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This event is often used to cancel the sorting operation or to perform a custom sorting routine.


How do you validate and retrieve data from database?

How do you validate and retrieve data from database?" How do you validate and retrieve data from database?"


Differentiate DATA from a database?

A database is a collection of data. Data represents items that are stored within the database.


Enter on the database or in the database?

We would use in rather than on. Data is stored in a database, not on a database. Data is entered into a database not onto a database.


Why do Database Management is related to MS Access?

Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.Access is a Database Management System, so it is related to the management of data. It is what a database is for.


What are to advantages of a database?

A database is a collection of interrelated data and the advantages of a database are ensured efficiency, standardized data, maintainable data, integrated data, reduced redundancy of data.


What are the roles of database and database administrators?

A database is a storage that contains data. To manage data in the database different roles are assigned . One such role is of a DBA that is database administrator that is responsible for managing the database.A database stores data and manages data. It allows people to enter data and get information from the data in the form of queries or reports.A database administrator looks after the maintenance of the database, including things like the design, testing, security etc. He services the needs of users that have to access the database.


Why do you have databases?

We have database to store data in to it. We prefer to have database over FPS because they handle the data efficiently.


Is 'A database uses characteristics such as field size and data type to define each field' false?

No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.No it is not false. Database fields do have field sizes and data types.


What are properties of database?

The interrelated data/records are stored in the database . Properties of database are that it maintains integrity of data, security of data, reduces problem in accessing the data.


What diff between the database and database system?

In simple terms, you can look at the database as being the files or tables with the data. A database system includes the other elements like queries, reports, forms, programs and other elements that manipulate and support the data in the database.