Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 10th, 2007, 04:29 AM
Registered User
Join Date: Jun 2007
Location: Lahore, Punjab, Pakistan.
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:


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

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

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

        If int <> 0 Then
            lblMessage.Text = int & "Record successfully added."
            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.
  #2 (permalink)  
Old July 10th, 2007, 08:12 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

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



================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
================================================== =========
I know that CVS was evil, and now i got the proof:
================================================== =========
  #3 (permalink)  
Old July 10th, 2007, 05:38 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

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.


================================================== =========
Read this if you want to know how to get a correct reply for your question:
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
================================================== =========
Why can't Programmers, program??
================================================== =========

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 12:13 AM.

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