Solved

asp.net - click button populate a gridview from db

Posted on 2010-08-24
3
617 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

749 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