I had to add another table into my database and now I need to go back and update a page that allows inserts into more than one table. I didn't write this page, so I'm trying to clean everything up, but there are some parts that I don't really understand. Now I have broken the page and it only inserts into one table. The very first one that I insert into.
ProductName: goes into the Product table
Description: goes into the Picklist table as Data.....it also is supposed to generate an insert into the marketing table based on the PicklistID which is an identity column. The marketing table tells the Picklist table that it is looking for a description.
Price: goes into Product table
Category: goes into the CategoryLink table which also inserts the recently generated ProductID.
Company: goes into the CompanyLink table which also inserts the recently generated ProductID.
Target audience: goes into the TargetLink table which also inserts the recently generated ProductID.
Status: goes into the Product table
Protected Sub submitButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles submitButton.Click
Dim CategoryID As String = txtCategoryID.Value
Dim CompanyIDs As New ArrayList
'Get selected companies-----
Dim CompanyCount As Integer = CompanyCheckbox.Items.Count
For i As Integer = 0 To CompanyCount - 1
If CompanyCheckbox.Items(i).Selected Then
CompanyIDs.Add(CompanyCheckbox.Items(i).Value)
End If
Next
'Get selected targets---
Dim TargetIDs As New ArrayList
Dim TargetCount As Integer = TargetCheckbox.Items.Count
For i As Integer = 0 To TargetCount - 1
If TargetCheckbox.Items(i).Selected Then
TargetIDs.Add(TargetCheckbox.Items(i).Value)
End If
Next
'Get Status---
Dim Status As String = Nothing
If StatusCheckbox.Checked Then
Status = "1"
Else
Status = "0"
End If
'SQL Insert: Product Table
Dim sqlInsertProduct As String = "IF NOT EXISTS (SELECT * FROM Product
WHERE ProductName= @ProductName)
BEGIN INSERT INTO Product
(ProductName, Status, CreateDate, ModifyDate, CreateUser, ModifyUser, Price)
VALUES (@ProductName, @Status ,getdate(),getdate(), @CreateUser, @ModifyUser,
@Price) END;
INSERT INTO Picklist (Title, Data) VALUES ('About this product', @Data);"
'Create SQL Connection
Using cn As New
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings
("LocalSqlServer").ConnectionString)
Using cmd As New SqlCommand(sqlInsertProduct, cn)
cmd.Parameters.Add(New SqlParameter("@ProductName",
txtNewProductName.Text))
cmd.Parameters.Add(New SqlParameter("@Status", StatusCheckbox.Checked))
cmd.Parameters.Add(New SqlParameter("@Price", txtPrice.Text))
cmd.Parameters.Add(New SqlParameter("@Data", txtProductDesc.Text))
cmd.Parameters.Add(New SqlParameter("@CreateUser",
System.Web.HttpContext.Current.User.Identity.Name))
cmd.Parameters.Add(New SqlParameter("@ModifyUser",
System.Web.HttpContext.Current.User.Identity.Name))
cn.Open()
cmd.ExecuteNonQuery()
End Using
'Get the productID of the newly inserted product
Dim sqlGetID As String = "SELECT @@IDENTITY"
Dim ProductID As String = Nothing
Dim cmdGetID As New SqlCommand(sqlGetID, cn)
Dim myReader As SqlDataReader = cmdGetID.ExecuteReader()
While myReader.Read
If IsDBNull(myReader(0)) Then
ProductID = ""
Else
ProductID = myReader(0)
End If
End While
myReader.Close()
cn.Close()
'SQL Insert: Marketing Table
Dim sqlInsertMarketing As String = "INSERT INTO Marketing (ProductID,
MarketingTypeID, MarketingTitle, MarketingData) VALUES ('" & ProductID & "', 2,
'Description', scope_identity())"
'SQL Insert: Category Table
If CategoryID <> Nothing Then
Dim sqlInsertCategory As String = "INSERT INTO CategoryLink (CategoryID,
ProductID) VALUES (@CategoryID,'" & ProductID & "')"
Using cmdInsertCategory As New SqlCommand(sqlInsertCategory, cn)
cmdInsertCategory.Parameters.Add(New SqlParameter("@CategoryID",
txtCategoryID.Value))
cn.Open()
cmdInsertCategory.ExecuteNonQuery()
End Using
cn.Close()
End If
If CompanyIDs.Count > 0 Then
For i = 0 To CompanyIDs.Count - 1
Dim sqlInsertCompany = "INSERT INTO CompanyLink (CompanyID, ProductID)
VALUES ('" & CompanyIDs(i) & "','" & ProductID & "')"
Using cmdInsertCompany As New SqlCommand(sqlInsertCompany, cn)
cn.Open()
cmdInsertCompany.ExecuteNonQuery()
End Using
cn.Close()
Next
End If
If TargetIDs.Count > 0 Then
For i = 0 To TargetIDs.Count - 1
Dim sqlInsertTarget = "INSERT INTO TargetLink (TargetID, ProductID)
VALUES ('" & TargetIDs(i) & "','" & ProductID & "')"
Using cmdInsertTarget As New SqlCommand(sqlInsertTarget, cn)
cn.Open()
cmdInsertTarget.ExecuteNonQuery()
End Using
cn.Close()
Next
End If
End Using
Response.Write("<script type='text/javascript'>{ alert('Product added successfully');
document.location.href = 'AddProduct.aspx'; }</script>")
End Sub
End Class
Like I said before, only the insert that goes into the Product table works. Before I added the Picklist table and tried to reconnect everything, this entire page worked. The code was a lot sloppier and didn't have parameters, so that could also be where I messed up since I am still trying to learn how to use them. If I need to include additional information I can do that. I don't know how detailed to make this post. Thanks
UPDATE:
I have gotten everything to INSERT except for the INSERT to the Marketing table. I would really appreciate someone who can help me get that PicklistID to insert into the MarketingData column of the Marketing table