 |
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
|
|
|
|
|

April 16th, 2010, 05:27 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 16th, 2010, 05:27 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
To what chapter / page is this related?
Imar
|
|

April 16th, 2010, 05:49 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 16th, 2010, 06:20 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 23rd, 2010, 11:42 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
|
|
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
|
|

April 26th, 2010, 04:17 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by jack_hilary
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()
|
|

April 26th, 2010, 10:48 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
|
|
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.
|
|
 |