•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 374,007 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,907 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 2399 | Replies: 52
![]() |
I'm trying to insert data into three tables in a relational database. Right now it will only insert to the first table and then it errors out. If you have some genius to share, don't hold back. Thanks.
Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Public Class DisAddCo
Inherits System.Web.UI.Page
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
' Retrieve controls
Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)
Dim customerid As Integer = 0
If CompanyTextBox Is Nothing Then
Return
End If
If FirstNameTextBox Is Nothing Then
Return
End If
If LastNameTextBox Is Nothing Then
Return
End If
If Address1TextBox Is Nothing Then
Return
End If
If CityTextBox Is Nothing Then
Return
End If
If StateTextBox Is Nothing Then
Return
End If
If ZipTextBox Is Nothing Then
Return
End If
If PhoneTextBox Is Nothing Then
Return
End If
' Set-up command
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)
Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
cmd.Parameters.AddWithValue("Company", CompanyTextBox.Text)
cmd.Parameters.AddWithValue("Address1", Address1TextBox.Text)
cmd.Parameters.AddWithValue("Address2", Address2TextBox.Text)
cmd.Parameters.AddWithValue("Phone", PhoneTextBox.Text)
Try
conn.Open()
Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Finally
If conn IsNot Nothing Then
conn.Close()
End If
End Try
' Do your second insert here
Dim cmd2 As New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
cmd2.Parameters.AddWithValue("cusID", customerid)
'cmd2.Parameters.AddWithValue("contactID", FirstNameTextBox.Text)
cmd2.Parameters.AddWithValue("FirstName", FirstNameTextBox.Text)
cmd2.Parameters.AddWithValue("LastName", LastNameTextBox.Text)
Try
conn.Open()
Dim contactID As String = cmd2.ExecuteScalar().ToString()
Finally
If conn IsNot Nothing Then
conn.Close()
End If
End Try
' Do your third insert here
Dim cmd3 As New SqlCommand("INSERT INTO [Zip] ([cusID], [zipID], [City], [State], [Zip]) VALUES (@cusID, @zipID, @City, @State, @Zip); SELECT SCOPE_IDENTITY();", conn)
cmd3.Parameters.AddWithValue("cusID", customerid)
cmd3.Parameters.AddWithValue("City", CityTextBox.Text)
cmd3.Parameters.AddWithValue("State", StateTextBox.Text)
cmd3.Parameters.AddWithValue("Zip", ZipTextBox.Text)
Try
conn.Open()
Dim zipID As Integer = Convert.ToInt32(cmd3.ExecuteScalar())
Finally
If conn IsNot Nothing Then
conn.Close()
End If
End Try
End Sub
End Class
•
•
Join Date: Sep 2007
Posts: 1,054
Reputation:
Rep Power: 3
Solved Threads: 61
Here you go:
Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Public Class DisAddCo
Inherits System.Web.UI.Page
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
' Retrieve controls
Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)
Dim customerid As Integer = 0
If CompanyTextBox Is Nothing Then
Return
End If
If FirstNameTextBox Is Nothing Then
Return
End If
If LastNameTextBox Is Nothing Then
Return
End If
If Address1TextBox Is Nothing Then
Return
End If
If CityTextBox Is Nothing Then
Return
End If
If StateTextBox Is Nothing Then
Return
End If
If ZipTextBox Is Nothing Then
Return
End If
If PhoneTextBox Is Nothing Then
Return
End If
' Set-up command
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)
Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone)", conn)
cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)
Dim cmd2 As New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName)", conn)
cmd2.Parameters.AddWithValue("@cusID", customerid)
'cmd2.Parameters.AddWithValue("@contactID", FirstNameTextBox.Text)
cmd2.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
cmd2.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)
Dim cmd3 As New SqlCommand("INSERT INTO [Zip] ([cusID], [zipID], [City], [State], [Zip]) VALUES (@cusID, @zipID, @City, @State, @Zip)", conn)
cmd3.Parameters.AddWithValue("@cusID", customerid)
cmd3.Parameters.AddWithValue("@City", CityTextBox.Text)
cmd3.Parameters.AddWithValue("@State", StateTextBox.Text)
cmd3.Parameters.AddWithValue("@Zip", ZipTextBox.Text)
Try
conn.Open()
cmd.ExecuteNonQuery()
cmd = New SqlCommand("SELECT SCOPE_IDENTITY()", conn)
Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
cmd2.ExecuteNonQuery()
cmd2 = New SqlCommand("SELECT SCOPE_IDENTITY()", conn)
Dim contactID As String = cmd2.ExecuteScalar().ToString()
cmd3.ExecuteNonQuery()
cmd3 = New SqlCommand("SELECT SCOPE_IDENTITY()", conn)
Dim zipID As Integer = Convert.ToInt32(cmd3.ExecuteScalar())
Finally
If conn.State = ConnectionState.Open Then conn.Close()
End Try
End Sub
End Class You are a helpy helperton for sure, but I did get this:
Server Error in '/HRIService' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30451: Name 'ConnectionState' is not declared.
Source Error:
Line 80: Dim zipID As Integer = Convert.ToInt32(cmd3.ExecuteScalar())
Line 81: Finally
Line 82: If conn.State = ConnectionState.Open Then conn.Close()
Line 83: End Try
Line 84: End Sub
Server Error in '/HRIService' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30451: Name 'ConnectionState' is not declared.
Source Error:
Line 80: Dim zipID As Integer = Convert.ToInt32(cmd3.ExecuteScalar())
Line 81: Finally
Line 82: If conn.State = ConnectionState.Open Then conn.Close()
Line 83: End Try
Line 84: End Sub
•
•
Join Date: Sep 2007
Posts: 1,054
Reputation:
Rep Power: 3
Solved Threads: 61
oops, I think I switched that one around. Replace line 82 with this:
I don't do codebehind coding all because of that stuff. Anyway, there you go ^^
If conn.State = System.Data.ConnectionState.Open Then conn.Close()
That fixed that but , there's always a but, I got this:
Object cannot be cast from DBNull to other types.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.
Source Error:
Line 70: cmd.ExecuteNonQuery()
Line 71: cmd = New SqlCommand("SELECT SCOPE_IDENTITY()", conn)
Line 72: Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Line 73:
Line 74: cmd2.ExecuteNonQuery()
I'm getting some stupid error in SQL about not being able to let certain fields be null even though they really aren't.
Object cannot be cast from DBNull to other types.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.
Source Error:
Line 70: cmd.ExecuteNonQuery()
Line 71: cmd = New SqlCommand("SELECT SCOPE_IDENTITY()", conn)
Line 72: Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Line 73:
Line 74: cmd2.ExecuteNonQuery()
I'm getting some stupid error in SQL about not being able to let certain fields be null even though they really aren't.
I have this crap now ... the db is set up to not allow nulls.
Server Error in '/HRIService' Application.
Cannot insert the value NULL into column 'contactID', table 'HRIService.dbo.Customers'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'contactID', table 'HRIService.dbo.Customers'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Source Error:
Line 68: conn.Open()
Line 69:
Line 70: cmd.ExecuteNonQuery()
Line 71: cmd = New SqlCommand("SELECT SCOPE_IDENTITY()", conn)
Line 72: Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Source File: C:\Inetpub\wwwroot\HRIService\ServiceExpress\DisAddCo.aspx.vb Line: 70
Stack Trace:
Server Error in '/HRIService' Application.
Cannot insert the value NULL into column 'contactID', table 'HRIService.dbo.Customers'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'contactID', table 'HRIService.dbo.Customers'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Source Error:
Line 68: conn.Open()
Line 69:
Line 70: cmd.ExecuteNonQuery()
Line 71: cmd = New SqlCommand("SELECT SCOPE_IDENTITY()", conn)
Line 72: Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Source File: C:\Inetpub\wwwroot\HRIService\ServiceExpress\DisAddCo.aspx.vb Line: 70
Stack Trace:
•
•
Join Date: Sep 2007
Posts: 1,054
Reputation:
Rep Power: 3
Solved Threads: 61
Well when you fix that portion, here's the new code:
Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Public Class DisAddCo
Inherits System.Web.UI.Page
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
' Retrieve controls
Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)
Dim customerid As Integer = 0
If CompanyTextBox Is Nothing Then
Return
End If
If FirstNameTextBox Is Nothing Then
Return
End If
If LastNameTextBox Is Nothing Then
Return
End If
If Address1TextBox Is Nothing Then
Return
End If
If CityTextBox Is Nothing Then
Return
End If
If StateTextBox Is Nothing Then
Return
End If
If ZipTextBox Is Nothing Then
Return
End If
If PhoneTextBox Is Nothing Then
Return
End If
' Set-up command
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)
Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)
Dim cmd2 As New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
cmd2.Parameters.AddWithValue("@cusID", customerid)
'cmd2.Parameters.AddWithValue("@contactID", FirstNameTextBox.Text)
cmd2.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
cmd2.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)
Dim cmd3 As New SqlCommand("INSERT INTO [Zip] ([cusID], [zipID], [City], [State], [Zip]) VALUES (@cusID, @zipID, @City, @State, @Zip); SELECT SCOPE_IDENTITY();", conn)
cmd3.Parameters.AddWithValue("@cusID", customerid)
cmd3.Parameters.AddWithValue("@City", CityTextBox.Text)
cmd3.Parameters.AddWithValue("@State", StateTextBox.Text)
cmd3.Parameters.AddWithValue("@Zip", ZipTextBox.Text)
Try
conn.Open()
Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Dim contactID As String = cmd2.ExecuteScalar().ToString()
Dim zipID As Integer = Convert.ToInt32(cmd3.ExecuteScalar())
Finally
If conn.State = ConnectionState.Open Then conn.Close()
End Try
End Sub
End Class![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb ASP.NET Marketplace
Similar Threads
- insert to multiple table (MS SQL)
- php script for dropdown to fetch DB list and then Tables (PHP)
- How to insert data in one column in database? (ColdFusion)
- recordset problem (ASP)
- ADODB.Recordset (0x800A0E78):Operation is not allowed when the object is closed. (ASP)
- updating multiple tables (ASP)
- dreamweaver question (Graphics and Multimedia)
- How to auto increment? (ASP.NET)
- Copy tables from one MySql DB to another MySql DB (MySQL)
Other Threads in the ASP.NET Forum
- Previous Thread: hide/show part of form using client side code for ASP.NET
- Next Thread: How to register page for a web user control codebehind??



Linear Mode