Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 16th, 2010, 05:27 AM
Authorized User
 
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Connect TextBoxes to Database

Hello!

I am building a form but I have problems connecting it to my database. The ConnectionString works fine and I am able to connect to it, but I do not know what to write in the .vb-file to connect it.

What I am trying to do is to send information from TextBoxes to the database, as well as loading information from the database into TextBoxes. I have given every TextBox an ID, but as mentioned I don't know what to write.

An additional problem I have is that I have done a generalization in the database, and I don't know how to handle it. For example, I have a table called Customer, with two tables called PrivateCustomer and BusinessCustomer connected to it through a generalization.

I am sort of new at VB.NET so please excuse my lack of knowledge.

Cheers!
Peter
 
Old April 16th, 2010, 05:27 AM
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,

To what chapter / page is this related?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 16th, 2010, 05:49 AM
Authorized User
 
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar!

Thank you for answering so fast!

I am looking at Chapter 9 at this moment. How do I connect a TextBox to a specific row in a database. Here's an example:

I have a three tables called Customer, PrivateCustomer and BusinessCustomer. PrivateCustomer has a row called FirstName. In my aspx-file I have a TextBox called FirstName. How do I connect the TextBox to that specific row in PrivateCustomer?

The problem though is that PrivateCustomer and BusinessCustomer are both generalizations of the table Customer. Both share the same primary key but have specific values that differs them.

Hope you understand what I mean.

Peter
 
Old April 16th, 2010, 06:20 AM
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,

What underlying model are you using? LINQ to SQL? When you say "generalizations", do you mean in the database, or in an object oriented object model?

Depending on your setup and model, you have a few options.

When using the SqlDataSource, you could use a JOIN query to get data from both tables.

Alternatively, you could use stored procedures to handle the multi-table update.

Finally, you could write plain ADO.NET code to handle database interaction:

http://quickstarts.asp.net/Quickstar...wcontents.aspx

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 23rd, 2010, 11:42 AM
Friend of Wrox
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Arrow

hi there try this one

Code:
string connectionString = <your connection string>
string insertSql = "INSERT INTO FirstName(FirstName) VALUES(@FirstName)";

using (SqlConnection myConnection = new SqlConnection(connectionString))
      {
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
                myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text);
                                             
                myCommand.ExecuteNonQuery();

                myConnection.Close();
            }
Hope this helps.

Cheers,

Jack
__________________
Jack: Founder, Developer & Owner Of JackAndGenieForever.Com
 
Old April 26th, 2010, 04:17 AM
Authorized User
 
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by jack_hilary View Post
hi there try this one

Code:
string connectionString = <your connection string>
string insertSql = "INSERT INTO FirstName(FirstName) VALUES(@FirstName)";

using (SqlConnection myConnection = new SqlConnection(connectionString))
      {
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
                myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text);
                                             
                myCommand.ExecuteNonQuery();

                myConnection.Close();
            }
Hope this helps.

Cheers,

Jack
Hi!

I'm writing in VB, not C# (that code is C# right?).

I suppose this is the corresponding code for VB?

Code:
string connectionString = s001ConnectionString
string insertSql = "INSERT INTO FirstName(FirstName) VALUES(@FirstName)"

using (SqlConnection myConnection = new
SqlConnection(connectionString))
myConnection.Open()
SqlCommand myCommand = new SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text)
myCommand.ExecuteNonQuery()
myConnection.Close()
 
Old April 26th, 2010, 10:48 AM
Friend of Wrox
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Arrow

Quote:
(that code is C# right?).
Yes you are right. But the code that you wrote is not correct. Try the code below, and let me know the issue, if any.

Code:
Dim connectionstring As String = <your connection string> 'your string in surrounded by double quotes and do not end with ;
Dim insertSql As String = "INSERT INTO FirstName(FirstName) VALUES(@FirstName)"
Using myConnection As New SqlConnection(connectionString)
    myConnection.Open()
   
    Dim myCommand As New SqlCommand(insertSql, myConnection)
    myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text)
   
    myCommand.ExecuteNonQuery()
   
    myConnection.Close()
End Using
Hope this helps.
__________________
Jack: Founder, Developer & Owner Of JackAndGenieForever.Com





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to connect database madu Java GUI 1 December 3rd, 2007 11:54 PM
Connect to remote database sankar Pro VB Databases 0 July 31st, 2003 01:43 AM
can't connect to database asprookie Classic ASP Databases 1 July 22nd, 2003 10:11 AM
Connect Database vikaspalic Classic ASP Databases 3 June 5th, 2003 09:02 PM





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