Solved

asp.net - click button populate a gridview from db

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

Join & Write a Comment

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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

10 Experts available now in Live!

Get 1:1 Help Now