I have built a data base in SQLServer 2008r2 that stores all the information pertaining to managing a montly budget, including stored procedures and views for updating and viewing the data.
I am trying to build a Web front end that will eventally be capaible of displaying each month's set of data and allow for updating.
At this stage I am just trying to display data into different gridviews (gridview1 and gridview2) which will show all the bills in pay period 1 and pay period 2.
I have a stored procedure that will accept variables @month and @payper to select the month id and pay period id.
I can get the data to display but I don't want to create the same code over and over again; This is where I am stuck, creating a method or class to call the data to grid view with the different variables.
I seem to be close by the GridView1.DataSource = ds doesn't work when I try to do this as a class.
The aspx page
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Budget.WebForm1" %>
Aspx.vb file
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = GetConnect()
con.Open()
'Dim str As String = "p_DisplayBills 1, 1"
'Dim cmd As New SqlCommand(str, con)
'Dim da As New SqlDataAdapter(cmd)
'Dim Ds As New DataSet()
'da.Fill(Ds, "p_DisplayBills")
'GridView1.DataSource = Ds
'GridView1.DataBind()
'Dim str2 As String = "p_DisplayBills 1, 2"
'Dim cmd2 As New SqlCommand(str2, con)
'Dim da2 As New SqlDataAdapter(cmd2)
'Dim ds2 As New DataSet()
'da2.Fill(ds2, "p_DisplayBills")
''GridView2.DataSource = ds2
''GridView2.DataBind()
Dim GridView1DataDisplay As New DataDisplay
GridView1DataDisplay.Display = "p_DisplayBills"
GridView1DataDisplay.DisplayD = "p_DisplayBills 1, 2"
GridView1.DataSource = Ds
GridView1.DataBind()
con.Close()
End Sub
End Class
The Class file
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class DataDisplay
Public Property DisplayD As String
Public Property Display As String
Public Sub DataDisplay()
Dim str As String = DisplayD
Dim cmd As New SqlCommand(str, con)
Dim Ds As New DataSet()
Dim da As New SqlDataAdapter(cmd)
da.Fill(Ds, Display)
End Sub
End Class
The connection method
Imports System
Imports System.Data
Imports System.Data.SqlClient
Module Connect
Public con As SqlConnection
Public Function GetConnect()
con = New SqlConnection(ConfigurationManager.ConnectionStrings("DB001").ConnectionString)
Return con
End Function
End Module
Maybe you need to set gridview Autogeneratecolumn property to true
Thanks for the reply. That didn't fix the problem.
This is the actual error. I am not sure how to be able to use DS outside of the Class DataDisplay
I found my answer...
I need to call DataDisplay as a Function and return ds....
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = GetConnect()
con.Open()
Dim gvd1 As New DataDisplay
gvd1.Display = "p_DisplayBills"
gvd1.DisplayD = "p_DisplayBills 1, 1"
GridView1.DataSource = gvd1.DataDisplay()
GridView1.DataBind()
Dim gvd2 As New DataDisplay
gvd2.Display = "p_DisplayBills"
gvd2.DisplayD = "p_DisplayBills 1, 2"
GridView2.DataSource = gvd2.DataDisplay()
GridView2.DataBind()
con.Close()
GridView1.Attributes.Add("class", "table table-striped")
GridView2.Attributes.Add("class", "table table-striped")
'GridView3.Attributes.Add("class", "table table-striped")
End Sub
End Class
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class DataDisplay
Public Property DisplayD As String
Public Property Display As String
Public Function DataDisplay()
Dim str As String = DisplayD
Dim cmd As New SqlCommand(str, con)
Dim Ds As New DataSet()
Dim da As New SqlDataAdapter(cmd)
da.Fill(Ds, Display)
Return Ds
End Function
End Class
沒有留言:
張貼留言