'Get the connection string, In this case it's comming from the web.config file
Dim strCon As String = System.Configuration.ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim con As New SqlConnection(strCon)
Dim cmd As New SqlCommand("[SEL_Results]", con)
cmd.CommandType = Data.CommandType.StoredProcedure
Dim yourParameter As SqlParameter = cmd.Parameters.Add("@yourParameter", Data.SqlDbType.VarChar)
yourParameter.Value = theValue
'Setting the dataAdapter to the sqlCommand.
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet
Try
con.open()
da.Fill(ds)
Catch ex As Exception
Finally
con.Close()
End Try
Dim dc As DataColumn
If ds.Tables.Count > 0 Then
'Building all the columns in the table.
For Each dc In ds.Tables(0).Columns
Dim bField As New BoundField
'Initalize the DataField value.
bField.DataField = dc.ColumnName
'Initialize the HeaderText field value.
bField.HeaderText = dc.ColumnName
'Add the newly created bound field to the GridView.
grdView.Columns.Add(bField)
Next
End If
'Setting the dataSource of the grid here.
grdView.DataSource = ds.Tables(0)
grdView.dataBind()
'The following example demonstrates paging and sorting in the gridview
private string GetSortDirection()
{
switch (GridViewSortDirection)
{
case "ASC":
GridViewSortDirection = "DESC";
break;
case "DESC":
GridViewSortDirection = "ASC";
break;
}
return GridViewSortDirection;
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGridControl();
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}
protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
}
else
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
}
}
return dataView;
}
else
{
return new DataView();
}
}