Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 3rd, 2006, 03:54 AM
Authorized User
 
Join Date: Apr 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default retrieveing values from db

Hi,

I'm looking for a way to get values from the db without using the drag and drop stuff in VWD Express. Or rather, I like D'n'D, it's just that I want to get values into the page load event (code behind; VB) and I think this must be achieved in another way. Someone suggested using a data reader but I don't know how to do it.

I'm used to good old Asp and I'm not afraid of writing my own code, but it seems strange to me to write code for such easy things, instead of using built-in functionality.

Similarily, I'm looking for a good way to get just one value to a text label or so, and I understand this could be done using Eval. But how do I get a value for Eval (i.e. a value from a SqlDataSource or the like?) BTW, I think this should be achieved by dragging a field from one of the tables from the database explorer, instead of getting a gridview for this one element (which seems rather stupid to me).

Thanks in advance!

Pettrer, Sweden

Coding is indeed a nine-to-five job; nine pm to five am.
__________________
Coding is indeed a nine-to-five job; nine pm to five am.
 
Old April 4th, 2006, 08:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Hi Pettrer,
Yes, a datareader is probably the way to go, but there are many options.

http://samples.gotdotnet.com/quickst...ooverview3.src

You can do pretty much anything without D'n'D but its hard to beleive with the way things are going in .NET.

Eval is for list controls such as GridView, Repeater, etc.
Dragging a field on to a form will generate such a control.
To retreive just one value for a database you can use a ExecuteScalar()
Code:
Dim conn As SqlConnection =     New SqlConnection(ConfigurationSettings.AppSettings("YOUR_CONNECTION_STRING_SETTING"))
Dim cmd As SqlCommand = New SqlCommand("SELECT MAX(Id) from Table", conn)
conn.Open()
Dim id As String = cmd.ExecuteScalar
conn.Close()
Using a datasource(reader/dataset/etc) is also possible but probably quite resource heavy for one field.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old April 4th, 2006, 08:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Check out this page for a general ADO.NET stuff.
Its not the best but will give you some ideas to look into further.
http://samples.gotdotnet.com/quickst...soverview.aspx

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old April 5th, 2006, 03:53 AM
Authorized User
 
Join Date: Apr 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I'll try this. Thank you som much for your help!

Pettrer

Coding is indeed a nine-to-five job; nine pm to five am.
 
Old April 6th, 2006, 09:52 AM
Authorized User
 
Join Date: Apr 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again,

Thanks for your reply. However, I still can't get it to work, mostly because I don't know what goes where. I use code-behind, and maybe that's problematic here. First, I included the following at the top of the code-behind page to reduce the errors i first got:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Then, VWD told me the connectionstring thing was obsolete and it surely made the app crash on me. I have defined a connectionstring called ekodbConnectionString, but when I tried your code I was told to update to the following:

Dim conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationMa nager.AppSettings(ekodbConnectionString As String))

which didn't work either. I then tried to declare it in the main page like this

<asp:SqlDataSource ID="nastavnr" runat="server"
     ConnectionString="<%$ ConnectionStrings:ekodbConnectionString %>"
     ProviderName="<%$ ConnectionStrings:ekodbConnectionString.ProviderNa me %>"
     SelectCommand="SELECT * FROM customers">
    </asp:SqlDataSource>

and then it still said the name was undeclared.

This is what I have now in Page_load, after several hours of googling and trying:

Dim conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationMa nager.AppSettings(ekodbConnectionString) As String)
Dim cmd As SqlCommand = New SqlCommand("SELECT id from customers", conn)
conn.Open()
Dim id As String = cmd.ExecuteScalar
conn.Close()

Could you please help me out?

Also, how should I get the id into the main page? Like this:

<%=id%>

?


Many thanks in advance,

Pettrer

Coding is indeed a nine-to-five job; nine pm to five am.
 
Old April 6th, 2006, 02:43 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Where did you define your connection string? Under appSettings, or under the connectionStrings node?

In ASP.NET 2, there is now a special connectionStrings node that allows you to set up your connection strings, so you have easy access to them.

Search Google for connectionStrings and ASP.NET to see what I mean...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old April 7th, 2006, 04:32 AM
Authorized User
 
Join Date: Apr 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Imar,

Not since I tried to code in Prolog(!) many years ago have I been so glad to see a simple "1" on my page! Thank you so much for your help. Just a question re your last post - you write that this node is something new and good for Asp.Net 2.0. Still, the book (Beginning Asp.Net 2.0) suggests using the webconfig setting, and it's also the default of VWD. So why shouldn't we use them?

For anyone reading this post and having the same problem as I did, here's what definitely worked for me:

In web.config:
<configuration>
    <appSettings/>
    <connectionStrings>
  <add name="ekodbConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;C:\Docu ments and Settings\Pettrer\My Documents\Visual Studio 2005\WebSites\eko\App_Data\ekodb.mdf&quot;;Integra ted Security=True;Connect Timeout=30;User Instance=True"
   providerName="System.Data.SqlClient" />
 </connectionStrings>
(etc.)

In Default2.aspx.vb:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings("ekodbConnectionString").C onnectionString)
        Dim cmd As SqlCommand = New SqlCommand("SELECT * from customers", conn)
        conn.Open()
        Dim id As String = cmd.ExecuteScalar
        conn.Close()
        Label1.Text = id
    End Sub

In Default2.aspx (in the body section):
        <asp:Label ID="Label1" runat="server" Style="z-index: 101; left: 320px; position: absolute;
            top: 100px" Text="Label"></asp:Label>

This yields the result of Label1's displayed text being 1. Actually, It's also easy to manipulate the id value in the Page Load, so I set:

Label1.Text = id + 1 (In reality, the SELECT statement will be something else; I simplified it here for testing purposes.)

I hope this explanation brought something back to rest of the community! Remember that you might need to adjust the code's wrapping to get it to work.

Pettrer

Coding is indeed a nine-to-five job; nine pm to five am.
 
Old April 7th, 2006, 12:22 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:Still, the book (Beginning Asp.Net 2.0) suggests using the webconfig setting, and it's also the default of VWD. So why shouldn't we use them?
Are you talking about the special <connectionStrings> setting? If so, then we all mean the same thing. In ASP.NET 1.x you had to store your connection string under the appSettings node. Now, with ASP.NET 2, there is a special <connectionStrings> setting that is used to store connection strings exclusively. The good thing about this section is that you can store additional information, like the type of the provider being used. It's indeed the location where VWD stores a new connection string

However, the code samples that you posted were all trying to use the AppSettings key (which has a reference to <appSettings> in the web.config file) for the connection string, which is something you shouldn't do.

If any book or article is suggesting you must use <appSettings> for connection strings, they are wrong.
Of course it will work, just like you can use that section to store other settings, but the <connectionStrings> node has been designed for it specifically.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old April 8th, 2006, 03:56 AM
Authorized User
 
Join Date: Apr 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again. I believe the book was correct then anyway, as it is indeed inside the conectionstrings tags now.

I think I learn a lot of Asp.Net every day, but doing the actual coding is still much harder than I thought, as there are so many options.

Pettrer

Coding is indeed a nine-to-five job; nine pm to five am.





Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieveing value through dataset thas123 ADO.NET 2 January 17th, 2006 08:50 PM
retrieveing a numeric value into a "variable" grant6607 XSLT 2 May 11th, 2005 11:08 AM
passing and displays values from a db w/ spaces mp213 PHP Databases 1 April 13th, 2005 09:26 AM
insert values into a MySQL DB from a drop down men crmpicco Classic ASP Basics 26 January 24th, 2005 08:10 AM
Unable to change DB values using ASP dghughes42 Classic ASP Databases 2 January 31st, 2004 02:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.