Solved

asp.net - click button populate a gridview from db

Posted on 2010-08-24
3
623 Views
Last Modified: 2013-11-07


On my web site I have a commandbutton, and when the user clicks on it...I need code that will go out and pull from 4 columns(date, server_name, file, results) from ms sql server 2005 and populate a gridview on my web page...

are there any samples?
0
Comment
Question by:GlobaLevel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 250 total points
ID: 33512433
Imports System.Data
Imports System.Data.SqlClient

On Button Click Event do This
DIm T as new Datatable
Dim Qry
Dim Conn
Connn="Your Sql Server Connection String"
Qry="SELECT [date],Server_name,file,results From YourTable"
Dim adt As new SqlDataadapter(Qry,Conn)
T.Clear
Adt.fill(t)
Me.Gridview1.Datasource=t
Me.gridview1.databind()
0
 
LVL 6

Assisted Solution

by:thaytu888888
thaytu888888 earned 250 total points
ID: 33512576
- Modify SelectCommand & ConnectionStrings:AppConnectionString1 to fit your case, you can
- Good article here: http://www.c-sharpcorner.com/UploadFile/raj1979/GridViewExamplesForASPNET2009162008055815AM/GridViewExamplesForASPNET20.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID">
            <Columns>
                <asp:BoundField DataField="Date" HeaderText="Date"
                    SortExpression="Date" />
                <asp:BoundField DataField="ServerName" HeaderText="ServerName" 
                    SortExpression="ServerName" />
                <asp:BoundField DataField="File" HeaderText="File" 
                    SortExpression="File" />
                <asp:BoundField DataField="Results" HeaderText="Results" 
                    SortExpression="Results" />               
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnView" runat="server" Text="View" onclick="btnView_Click" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" 
            SelectCommand="SELECT YourColumns FROM [YourTableName]"></asp:SqlDataSource>

    </div>
    </form>
</body>
</html>

//Code behind
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            GridView1.DataSourceID = SqlDataSource1.ID;
            GridView1.DataBind();
        }
    }
}

Open in new window

0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 33532850
This is working... Any way for the gridview to populate on page load... FYI.. All the code is retained.. Inside the Asp:control...no code behind
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question