Solved

Change gridview datasource to connection sting in web.config. ASP.Net 2.0, VS2005, VB.Net, Access Database

Posted on 2007-11-24
9
3,119 Views
Last Modified: 2010-05-18
I guess this is so basic no one shows how to do it...

The host I use wants me to use a direct path to the database. When I drag a gridview onto the page, it asks for a datasource. I enter the information and everything works fine.

However, I've read that you shouldn't have the physical path in the aspx file, it should be in the webconfig file. So, I need to know how to convert this to the webconfig file:

 <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="C:\myDirectory\database\events.mdb"
            DeleteCommand="DELETE FROM [events] WHERE [eventID] = ?" InsertCommand="INSERT INTO [events] ([eventID], [eventName], [eventDate], [eventTime], [eventAddress], [eventCity], [eventState], [eventPhone], [eventDescription]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
            SelectCommand="SELECT * FROM [events] ORDER BY [eventID] DESC" UpdateCommand="UPDATE [events] SET [eventName] = ?, [eventDate] = ?, [eventTime] = ?, [eventAddress] = ?, [eventCity] = ?, [eventState] = ?, [eventPhone] = ?, [eventDescription] = ? WHERE [eventID] = ?">
            <DeleteParameters>

Best,
MH
0
Comment
Question by:MHenry
  • 4
  • 3
  • 2
9 Comments
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 20343830
In webconfig file add a section named ConnectionString before web.config section:
<connectionStrings>
  <add name="mycs" connectionString="Data Source=38.157.64.125;Initial Catalog=dbname;User ID=sa; Password=123" />
 </connectionStrings>


Then in your page html source, bind the connection string property of your proper control to this cs using:
ConnectionString=<%$ ConnectionStrings:mycs %>
(You may use data source controls)
0
 
LVL 7

Author Comment

by:MHenry
ID: 20343865
Sorry, that's where you lose me. How do I add that to the code above? I know how to put it in the webconfig. What I don't know is how or where to put the other part.

Can you show me an example using the code above?

Thanks,
MH
0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 20343910
Here is a sample file:


<?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>

    <appSettings/>

  <connectionStrings>

    <add name="myCS" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E:\Mortaza\My Projects\Visual Basic Working Projects\Bell Telephone\Main\Database\Mrtdb.mdb'"/>

  </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="false" strict="false" explicit="true" />

        <pages>

            <namespaces>

                <clear />

                <add namespace="System" />

                <add namespace="System.Collections" />

                <add namespace="System.Collections.Specialized" />

                <add namespace="System.Configuration" />

                <add namespace="System.Text" />

                <add namespace="System.Text.RegularExpressions" />

                <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>

        </pages>

        <!--

            The <authentication> section enables configuration 

            of the security authentication mode used by 

            ASP.NET to identify an incoming user. 

        -->

        <authentication mode="Windows" />

        <!--

            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>

        -->

    </system.web>

</configuration>

Open in new window

0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 20343919
Sorry for misunderstanding, your aspx file should look like this:
(Assumed that your db file is copied to your web sites root directory)


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.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>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BlackListID"

            DataSourceID="AccessDataSource1">

            <Columns>

                <asp:BoundField DataField="BlackListID" HeaderText="BlackListID" InsertVisible="False"

                    ReadOnly="True" SortExpression="BlackListID" />

                <asp:BoundField DataField="Tel_vc" HeaderText="Tel_vc" SortExpression="Tel_vc" />

                <asp:BoundField DataField="Comments_vc" HeaderText="Comments_vc" SortExpression="Comments_vc" />

            </Columns>

        </asp:GridView>

    

    </div>

        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/Mrtdb.mdb"

            SelectCommand="SELECT [BlackListID], [Tel_vc], [Comments_vc] FROM [BlackList_T]">

        </asp:AccessDataSource>

    </form>

</body>

</html>

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Author Comment

by:MHenry
ID: 20344064
Mortaza_doulaty,

In my case, the above wont work. The database is above root level so I can't use the ~/database.mdb.

What I need is to use the connection string from the webconfig in the .aspx file. In the above, you show that the connection string is name="myCS"

Is there a way to use the myCS in the gridview code?

Thanks,
MH
0
 
LVL 10

Expert Comment

by:joex911
ID: 20344246
Hi, you cannot use a connection string with an AccessDataSource. What you need to do is place the file in the app_data folder so that it can be found using a relative path.
0
 
LVL 7

Author Comment

by:MHenry
ID: 20344296
joex911,

Not that I don't believe you, but I'll leave this open a while because I have absolutely no way of knowing. I guess I just do it the way the host suggests and let them worry about the security...

Best,
MH
0
 
LVL 10

Accepted Solution

by:
joex911 earned 500 total points
ID: 20344312
what you could do is used an SqlDataSource instead, that way you can use the connection string in the web.config file.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myCS %>" ProviderName="<%$ ConnectionStrings:myCS.ProviderName %>" SelectCommand="SSELECT [BlackListID], [Tel_vc], [Comments_vc] FROM [BlackList_T]"></asp:SqlDataSource>

Open in new window

0
 
LVL 7

Author Comment

by:MHenry
ID: 20344570
Perfect. That works!

Thanks joex911

Best,
MH
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.Net Web Site Password specs 2 21
Setting runtime form location 4 19
VB.NET HttpWebRequest 12 34
Re-position the objects 7 52
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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