Solved

How can I display one record from my datasource in a textbox

Posted on 2006-07-17
7
258 Views
Last Modified: 2008-02-01
Hello experts

I would like to display one row from my datasource (which has only one item due to a specific query) in a textbox.  Is this possible?  I am using an Access database, I am able to display records in gridview's etc.  But what I really want to do is to display one item in a textbox when a user clicks a button.  I am not too terribly interested in using a form view (although that does work), unless someone knows how to programatically determine the text inside of that form view after it is filled.

I am obviously just starting to get my feet wet with Visual Web Developer and ASP.net so please forgive my question if it is poorly asked.

I appreciate any input.
0
Comment
Question by:xfungalx
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:skvikram
ID: 17122544
<Script runat="Server">
Sub btnButton_Click( s As Object, e As EventArgs )
  Dim strRow As String
  strRow= '''PLACE YOUR CODE HERE TO GET SELECTED ROWS e.g. select * from [table] where [condition]
  txtTextBox.text= strRow
End Sub
</Script>
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17122798
Using a OldDbDataAdapter object if db is Access DB:

Dim ds new DataSet()
Dim myConnection As SqlConnection = New SqlConnection("server=(local)\VSdotNET;Trusted_Connection=yes;database=northwind")
Dim mySqlDataAdapter As SqlDataAdapter = New SqlDataAdapter("select * from customers WHERE CUSTOMER_ID=1", myConnection)
sqlDataAdapter.Fill(ds)


Response.Write (ds.Tables(0).Rows(0)("COLUMN_NAME")

More help on that is available at ASP.NET QuickStart tutorial:

ADO.NET Overview
http://samples.gotdotnet.com/quickstart/aspplus/doc/adoplusoverview.aspx

--Nauman.
0
 

Author Comment

by:xfungalx
ID: 17123181
Here is what I currently have in my vb code behind page.  

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ds As New Data.DataSet
        Dim myConnection As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection("server=(local)\VSdotNET;Trusted_Connection=yes;database=mydatabase.mdb")

        Dim mySqlDataAdapter As Data.SqlClient.SqlDataAdapter = New Data.SqlClient.SqlDataAdapter("SELECT Users.Password FROM Users WHERE (((Users.Password)=""r3dst4ff""));", myConnection)
       
        mySqlDataAdapter.Fill(ds)

        ShowDataBox.Text = (ds.Tables(0).Rows(0)("Username"))
    End Sub

I am getting this error :

An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I am almost certain that the error is originating from the connection string, particularly here: database=mydatabase.mdb"

Any ideas?  My database is in the App_Data folder.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 25

Accepted Solution

by:
nauman_ahmed earned 500 total points
ID: 17123503
You are using MDB therefore you have to use OleDbConnection, OleDbDataAdapter classes :)

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\Inetpub\MyDb.mdb")
Dim ds As New DataSet()
conn.open
Dim strSQL as String
strSQL = "SELECT Users.Password FROM Users WHERE (((Users.Password)=""r3dst4ff""))"
Dim da As New OleDbDataAdapter(strSQL,conn)
da.Fill(ds)

ShowDataBox.Text = (ds.Tables(0).Rows(0)("Username"))

-Nauman.
0
 

Author Comment

by:xfungalx
ID: 17124438
Ok Ahmed,

It all works assuming I provide the absolute path to my database (located on my C: drive, all the way up to my VS folder etc.).  However, I don't know exactly what that path will be when I upload the site to the server.  I imagine I could troubleshoot through trial and error at that point but a relative path would be nice.

You have answered the bulk of my question beautifully thus far.

Another quick question regarding using SQL server instead.  If I set up a SQL data connection within VWD will it work on the server with zero SQL configuration (server side) on my part?  I (like many people nowadays) pay monthly for a webserver (1and1.com).  If so, I will probably just go that route.

Also, regarding skvikram's suggestion, he wrote the code to appear directly on the aspx page, as opposed to doing it on the VB code behind form (i.e. <Script runat="Server"> and </script> tags).  What is the difference?  As I am far more familiar (and that aint saying much) and more comfortable writing straight VB.NET code to VB.NET forms, therefore I prefer to use the code behind (aspx.vb) pages.

I will wait for your reply, and then award points as your solution works just as I would like, sans the absolute/relative path.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17124671
1. The best approach here is to give the db path in web.config in appSettings block:

<configuration>
    <appSettings>
    <add key="DB_PATH" value="DB_PATH">
  </appSettings>

  <system.web>
   </sytem.web>
</configuration>

In your ASPX.VB page you can then reference it like:

Dim strConnectionStrng As String
strConnectionString = System.Configuration.ConfigurationSettings.AppSettings("DB_PATH")

When you upload the project to production, you just have to change the db path in web.config and everything will work smooth. For your convenience I recommend you have the following structure in web.config file so that you do not have to change anything after configuring the application first time:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
      <add key="CURRENT_CONFIG" value="LOCAL"></add>
      <add key="SQL_SERVER_CONNECTION_STRING_LOCAL" value="packet size=4096;user id=sa;data source=localhost;persist security info=True;initial catalog=PUBS;password=PASSWORD"></add>
      <add key="SQL_SERVER_CONNECTION_STRING_PRODUCTION" value="packet size=4096;user id=sa;data source=192.168.0.1;persist security info=True;initial catalog=PUBS;password=password"></add>
</appSettings>
</configuration>

In your code you can then look at the value of CURRENT_CONFIG, if it is LOCAL then use SQL_SERVER_CONNECTION_STRING_LOCAL, otherwise use SQL_SERVER_CONNECTION_STRING_PRODUCTION. You are using Microsoft Access so your connection string will be different. You should also consider a data layer that will make your life easy and you do not have to declare the DB objects again and again. Url for that is

Writing Data Access Layer
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20993347.html?query=Nauman+DatabaseManagement&clearTAFilter=true

To convert this code to VB.NET you can use code translator at http://carlosag.net/Tools/CodeTranslator/Default.aspx

2. If you do the configuration as I suggested, then your application will work with very minimal changes whenever you port application to the server.

3. Vikram has used an inline coding style. If you use VS.NET and code-behind than you do not have to use runat=server attribute in ASPX pages.

4. I also recommend the following structure for your web projects to make sure that your MS ACcess DB files are safe. Assuming you have a control on the server and the server root directory is located in D:\

D:\www.yourwebsite.com
   \Db
   \www
    \temp

HTH, Nauman

0
 

Author Comment

by:xfungalx
ID: 17125119
Top notch help, thanks a lot.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular JS Route 3 70
Connection String 16 57
Need help with building query string 2 19
localhost: This site can’t be reached  localhost refused to connect. 2 53
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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