Asked by:
Access DataSource Codebehind

Question
-
User14589008 posted
Hello,
I am having trouble getting my gridview to populate with access data. I added my access data source in my web config file. Now I am just tyring to call the connection string in my web config file in my codebehind file. The code is below:
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Load()Dim Panel1 As Panel
Dim Panel2 As Panel
Dim GridView1 As DataGrid
Dim dtCustomerInfo As Data.DataTable
dtCustomerInfo = GetQuery("SELECT * FROM resumetable")
GridView1.DataSource = dtCustomerInfo
GridView1.DataBind()Panel1.Visible = False
Panel2.Visible = True
End SubPrivate Function GetQuery(strSQL As String) As DataTable
Dim dbConn As Data.OleDb.OleDbConnection
Dim ExecuteReader as System.Data.OleDb.OleDbConnection
Dim cmdSQL As Data.OleDb.OleDbCommand
Dim dtResults As Data.DataTable
'dbConn = System.Configuration.ConfigurationManager.ConnectionStrings["accessConStr"].ConnectionStringdbConn = (New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\hosting/djbconsulting/access_db/buddyschampion.mdb"))
cmdSQL = New Data.OleDb.OleDbCommand(strSQL, dbConn)
dbConn.Open()
dtResults.Load(cmdSQL.ExecuteReader())
dbConn.Close()Return dtResults
End Function
End Class
Sunday, February 1, 2009 10:29 AM
All replies
-
User-1171043462 posted
try this
Private Function GetQuery(strSQL As String) As DataTable
Dim dbConn As Data.OleDb.OleDbConnection
Dim ExecuteReader as System.Data.OleDb.OleDbConnection
Dim cmdSQL As Data.OleDb.OleDbCommand
Dim dtResults As Data.DataTable
'dbConn = System.Configuration.ConfigurationManager.ConnectionStrings["accessConStr"].ConnectionStringdbConn = (New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\hosting/djbconsulting/access_db/buddyschampion.mdb"))
cmdSQL = New Data.OleDb.OleDbCommand(strSQL, dbConn)
dbConn.Open()
Dim sda as new Data.OleDb.OleDbDataAdapter
sda.SelectCommand = cmdSQL
sda.Fill(dtResults)dbConn.Close()
Return dtResults
End Function
Sunday, February 1, 2009 10:54 AM -
User14589008 posted
Thanks for getting back with me. I tried the "Dim sda..." code from above and it still does not work. It is not even giving me an error message to let me know what I am doing wrong. Here is my .aspx file for the codebehind file:
<%@ Page Language="VB" AutoEventWireup="False" CodeFile="resumesearch.aspx.vb" Inherits="_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>BUDDYSCHAMPION - Resume Search</title>
<style type="text/css">
.style1
{
font-size: x-large;
color: #FF0000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"
style="font-size: xx-large; font-weight: 700"
Text="BUDDYSCHAMPION - RESUME SEARCH"></asp:Label>
<br />
<br />
<span class="style1">CLICK THE HEADING LINK TO SORT<br />
<br /><asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="First Name" HeaderText="First Name"
SortExpression="First Name" />
<asp:BoundField DataField="Last Name" HeaderText="Last Name"
SortExpression="Last Name" />
<asp:BoundField DataField="Address1" HeaderText="Address1"
SortExpression="Address1" />
<asp:BoundField DataField="Address2" HeaderText="Address2"
SortExpression="Address2" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="School Name" HeaderText="School Name"
SortExpression="School Name" />
<asp:BoundField DataField="College Name" HeaderText="College Name"
SortExpression="College Name" />
<asp:BoundField DataField="Years Completed" HeaderText="Years Completed"
SortExpression="Years Completed" />
<asp:BoundField DataField="Degree" HeaderText="Degree"
SortExpression="Degree" />
<asp:BoundField DataField="Currently Working" HeaderText="Currently Working"
SortExpression="Currently Working" />
<asp:BoundField DataField="Job Title" HeaderText="Job Title"
SortExpression="Job Title" />
<asp:BoundField DataField="Months at Job" HeaderText="Months at Job"
SortExpression="Months at Job" />
<asp:BoundField DataField="Years at Job" HeaderText="Years at Job"
SortExpression="Years at Job" />
<asp:BoundField DataField="Job Category" HeaderText="Job Category"
SortExpression="Job Category" />
<asp:BoundField DataField="Expected Pay" HeaderText="Expected Pay"
SortExpression="Expected Pay" />
<asp:BoundField DataField="Available Start Date"
HeaderText="Available Start Date" SortExpression="Available Start Date" />
</Columns>
</asp:GridView></span>
</div>
</form>
</body>
</html>Sunday, February 1, 2009 11:23 AM -
User-1171043462 posted
Check whether ur datatable has rows in it by taking count
dim cnt as integer = dtResults.Rows.Count
Sunday, February 1, 2009 11:59 AM -
User14589008 posted
Hello,
I added the code, but I am not seeing a numerical value anywhere. Here is my new codebehind file:
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Load()Dim Panel1 As Panel
Dim Panel2 As Panel
Dim GridView1 As DataGrid
Dim dtCustomerInfo As Data.DataTable
dtCustomerInfo = GetQuery("SELECT * FROM resumetable")
GridView1.DataSource = dtCustomerInfo
GridView1.DataBind()Panel1.Visible = False
Panel2.Visible = True
End SubPrivate Function GetQuery(strSQL As String) As DataTable
Dim dbConn As Data.OleDb.OleDbConnection
Dim ExecuteReader as System.Data.OleDb.OleDbConnection
Dim cmdSQL As Data.OleDb.OleDbCommand
Dim dtResults As Data.DataTable
'dbConn = System.Configuration.ConfigurationManager.ConnectionStrings["accessConStr"].ConnectionStringdbConn = (New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\hosting/djbconsulting/access_db/buddyschampion.mdb"))
cmdSQL = New Data.OleDb.OleDbCommand(strSQL, dbConn)
dbConn.Open()Dim sda as new Data.OleDb.OleDbDataAdapter
sda.SelectCommand = cmdSQL
sda.Fill(dtResults)
'dtResults.Load(cmdSQL.ExecuteReader())
dbConn.Close()Return dtResults
dim cnt as integer = dtResults.Rows.Count
End Function
End Class
Sunday, February 1, 2009 3:16 PM -
User-1171043462 posted
try this and see the results will be printed on ur page
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Load()Dim Panel1 As Panel
Dim Panel2 As Panel
Dim GridView1 As DataGrid
Dim dtCustomerInfo As Data.DataTable
dtCustomerInfo = GetQuery("SELECT * FROM resumetable")Response.Write("Total Count = " & dtCustomerInfo .Rows.Count)
GridView1.DataSource = dtCustomerInfo
GridView1.DataBind()Panel1.Visible = False
Panel2.Visible = True
End SubPrivate Function GetQuery(strSQL As String) As DataTable
Dim dbConn As Data.OleDb.OleDbConnection
Dim ExecuteReader as System.Data.OleDb.OleDbConnection
Dim cmdSQL As Data.OleDb.OleDbCommand
Dim dtResults As Data.DataTable
'dbConn = System.Configuration.ConfigurationManager.ConnectionStrings["accessConStr"].ConnectionStringdbConn = (New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\hosting/djbconsulting/access_db/buddyschampion.mdb"))
cmdSQL = New Data.OleDb.OleDbCommand(strSQL, dbConn)
dbConn.Open()Dim sda as new Data.OleDb.OleDbDataAdapter
sda.SelectCommand = cmdSQL
sda.Fill(dtResults)
'dtResults.Load(cmdSQL.ExecuteReader())
dbConn.Close()Return dtResults
End Function
End Class
Sunday, February 1, 2009 11:40 PM -
User14589008 posted
Hello,
I added the code above and I am still unable to see the count. Please click this link here: http://buddyschampion.com/resumesearch.aspx. This is the page I am trying to get my grid view to appear on, but I don't see anything. This is really weird. Permissions are correct, as well.
I am thinking that this maybe has something to do with my web config file connection string. Would you mind taking a look, if you feel this may be the problem?
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings/>
<connectionStrings><remove name="accessConStr"/>
<add name="accessConStr"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\hosting/djbconsulting/access_db/buddyschampion.mdb"
providerName="System.Data.OleDb">
</add><remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="server=createaccount.db.2733880.hostedresource.com,1433; Initial Catalog=createaccount;uid=XXXXXXX;pwd=XXXXXXXXXX;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
<compilation debug="true" strict="false" explicit="true">
<identity impersonate="false" />
--><compilation debug="true" strict="false" explicit="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies></compilation>
<pages>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Generic" />
<add namespace="System.Collections.Specialized" />
<add namespace="System.Configuration" />
<add namespace="System.Text" />
<add namespace="System.Text.RegularExpressions" />
<add namespace="System.Linq" />
<add namespace="System.Xml.Linq" />
<add namespace="System.Web" />
<add namespace="System.Web.Caching" />
<add namespace="System.Web.SessionState" />
<add namespace="System.Web.Security" />
<add namespace="System.Web.Profile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.WebControls.WebParts" />
<add namespace="System.Web.UI.HtmlControls" />
</namespaces><controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls></pages>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Forms" /><!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<customErrors mode="Off"/>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web><system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4"
type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom><!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule" />
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory" />
<remove name="ScriptHandlerFactoryAppServices" />
<remove name="ScriptResource" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime><system.net>
<mailSettings>
<smtp from="BuddysChampion">
<network host="XXXXXXXXXXXX" password="XXXXXXXXXX"
userName="test@domain.com" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Monday, February 2, 2009 8:50 AM -
User-1171043462 posted
Check whether ur database table has values in it
Since there is no error this might be the issue
Monday, February 2, 2009 10:07 AM -
User14589008 posted
Hello,
Yes, there is data in the table. I don't know what's going on here. This is so weird!
Thanks for ALL of your help. This is really driving me crazy!!! :-)
Monday, February 2, 2009 4:02 PM -
User-1171043462 posted
just try this
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="True">Monday, February 2, 2009 11:42 PM -
User-821857111 posted
Dim Panel1 As Panel
Dim Panel2 As Panel
Dim GridView1 As DataGridGet rid of these three lines.
Tuesday, February 3, 2009 3:21 AM -
User14589008 posted
Done - still no error message(s).
Tuesday, February 3, 2009 9:10 AM -
User14589008 posted
Completed - but still nothing. I don't know what to do. Maybe it's ... ummmm...could it be something wrong with my web.config file/connection string?
Tuesday, February 3, 2009 9:11 AM -
User-1171043462 posted
Can you check thecount of rows in ur datatable??
Tuesday, February 3, 2009 9:40 AM -
User-821857111 posted
could it be something wrong with my web.config file/connection string?You aren't one of those people that has multiple copies of the database you are developing are you? You know - one with no data in it, and one filled up. Then you go and connect to the empty one?
Tuesday, February 3, 2009 12:19 PM -
User14589008 posted
I'm not that stupid. :-)
I tried a few other things and nothing.
I re-did the entire page on my local machine and the page views just fine. I don't know why I am not getting anything when viewing the page online. Is there another way to make my data showup on the page and allow sorting without using a gridview?
Friday, February 6, 2009 12:56 PM -
User-1593004730 posted
Devirell,
Did you get this to work?
You say it works on the local box...this is using the path to the file that you gave in a previous post, right?
On the website, you may have to change the path in your connection code to reflect the location of the database on the server. This is if the path to the database is different than the path on your local box.
Is the website and Access database on the same server?
Sunday, February 8, 2009 4:06 PM -
User-821857111 posted
I don't know why I am not getting anything when viewing the page onlineYou are getting something. You are getting an error message. First thing I wopuld do is move the database to the App_Data folder and then use the following connection string:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|buddyschampion.mdb"
That way, the connection string can stay the same regardless of which machine you are using, so you don't need to make any changes when uploading your code to the web server. See if that helps
Sunday, February 8, 2009 4:35 PM -
User-1171043462 posted
I'm not that stupid. :-)
I tried a few other things and nothing.
I re-did the entire page on my local machine and the page views just fine. I don't know why I am not getting anything when viewing the page online. Is there another way to make my data showup on the page and allow sorting without using a gridview?
Have you checked whether ur DataTable has Rows or not??
Sunday, February 8, 2009 11:44 PM -
User14589008 posted
Would someone be willign to give me a call to walk through a few examples? Please let me know. Thanks.
Monday, February 9, 2009 12:43 PM