Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 July 10th, 2007, 04:29 AM
Registered User
 
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mfarooqw
Default how Insert record into two tables

I have two tables, that is tblProduct and tblPacking.
i write a stored procedure two insert data in two tables which is:

CREATE PROCEDURE sp_add

@ProId varchar(50),
@Pro_Id varchar(50),
@Composition varchar(100),
@Our_Brand varchar(100),
@Generic varchar(100),
@Cat_id int,
@Pack_id int,
@Strength varchar(50),
@Pack_Type varchar(50),
@Qty int,
@Price float(8)
AS
Begin
Insert into tblProduct (Pro_Id, Composition, Our_Brand, Generic, Cat_id) Values (@ProId, @Composition, @Our_Brand, @Generic, @Cat_id);
Insert into tblPacking (Pack_id, Pro_id, Strength, Pack_Type, Qty, Price) Values (@Pack_id, @Pro_Id, @Strength, @Pack_Type, @Qty, @Price)
End
GO


and my code is:

Dim objConn As New SqlConnection("server=(local); uid=sa; pwd=; database=cheapbrandpharmacy")
If Page.IsValid Then
            Dim objcmd As New SqlCommand("sp_Add", objConn)
        objcmd.CommandType = CommandType.StoredProcedure
        Dim objParam As SqlParameter
        Dim int As Integer = 0
        objParam = objcmd.Parameters.Add("@Pro_id", OleDbType.VarChar)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = "CBP-" & txtPro_id.Text
        objParam = objcmd.Parameters.Add("@Our_Brand", OleDbType.VarChar)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtBrand.Text
        objParam = objcmd.Parameters.Add("@Composition", OleDbType.VarChar)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtComposition.Text
        objParam = objcmd.Parameters.Add("@Generic", OleDbType.VarChar)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtGeneric.Text
        objParam = objcmd.Parameters.Add("@Cat_id", OleDbType.VarChar)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtCat_id.Text
        objParam = objcmd.Parameters.Add("@Pack_id", OleDbType.Integer)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtPack_id.Text
        objParam = objcmd.Parameters.Add("@Strength", OleDbType.Integer)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtStrength.Text
        objParam = objcmd.Parameters.Add("@Pack_Type", OleDbType.VarChar)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtPack_Type.Text
        objParam = objcmd.Parameters.Add("@Quantity", OleDbType.Integer)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtQuantity.Text
        objParam = objcmd.Parameters.Add("@Price", OleDbType.VarChar)
        objParam.Direction = ParameterDirection.Input
        objParam.Value = txtPrice.Text

        Try
                objcmd.Connection.Open()
             int = objcmd.ExecuteNonQuery()
                objcmd.Connection.Close()
        Catch ex As Exception
            lblMessage.Text = ex.Message
        End Try

        If int <> 0 Then
            lblMessage.Text = int & "Record successfully added."
        Else
            lblMessage.Text = "No Record is added"
        End If
        End If
    End Sub

but the record is not inserted in both of tables.
Plz tell me what is problem in my code, I have great stress.
 
Old July 10th, 2007, 08:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hey there.. do you receive any error?? or it just not adding the record???

also.. if you get an error you are not reading it because you are using the same label to display the error and the result...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old July 10th, 2007, 05:38 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

lblmessage will ALWAYS display "No Record is added" regardless if an error is thrown or not. The reason is because int will always be 0 because:

int = objcmd.ExecuteNonQuery()

ExecuteNonQuery does not return a result set (why you didnt get an error is beyond me) and, further more, you don't have any stored proce varlabies set to Output. I think maybe you were thinking of ExecuteScalar but, again, you aren't returning a value so ExecuteScalar would be pointless.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Data into two tables Lofa PHP Databases 4 July 7th, 2007 10:36 PM
Insert record Into 2 related tables at once kalchev ASP.NET 2.0 Basics 2 May 9th, 2006 05:10 AM
Insert new Record priority05 Classic ASP Databases 2 September 6th, 2004 12:26 PM
Insert into different Tables cs9896 SQL Language 2 September 16th, 2003 03:52 AM





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