Solved

asp.net - click button populate a gridview from db

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now