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
Solved

asp.net - click button populate a gridview from db

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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