[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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

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
xfungalx
Asked:
xfungalx
  • 3
  • 3
1 Solution
 
skvikramCommented:
<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
 
nauman_ahmedCommented:
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
 
xfungalxAuthor Commented:
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
Independent Software Vendors: 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!

 
nauman_ahmedCommented:
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
 
xfungalxAuthor Commented:
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
 
nauman_ahmedCommented:
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
 
xfungalxAuthor Commented:
Top notch help, thanks a lot.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now