Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-07-17
7
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 25

Accepted Solution

by:
nauman_ahmed earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

618 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