Results 1 to 4 of 4

Thread: [RESOLVED] [2005] need help in using stored proc for insert command

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Resolved [RESOLVED] [2005] need help in using stored proc for insert command

    hi all,

    first of, here's what i'm using.

    Visual Basic 2005 Express Edition
    Oracle Database 10g Express Edition

    i created a procedure stored in the oracle database create_user and accepts a few parameters...

    Code:
    procedure create_user 
    ( p_user_num        out tbl_users.user_num%type
     ,p_username         in tbl_users.username%type
     ,p_password         in tbl_users.encrypted_password%type
     ,p_title            in tbl_users.title%type
     ,p_last_name        in tbl_users.last_name%type
     ,p_first_name       in tbl_users.first_name%type
     ,p_middle_name      in tbl_users.middle_name%type
     ,p_email_address    in tbl_users.email_address%type
     ,p_gender           in tbl_users.gender%type
     ,p_date_of_birth    in tbl_users.date_of_birth%type
     ,p_description      in tbl_users.description%type);
    in my visual basic project, i manually created a dataset and a datatable for the tbl_users table using the dataset designer since express edition doesn't support oracle for its dataset wizard.

    anyways, in my form i have a BindingNavigator and a BindingSource..

    and the following codes:

    Code:
        Private da As New OracleDataAdapter
        Private selCmd As New OracleCommand
        Private updCmd As New OracleCommand
        Private delCmd As New OracleCommand
        Private insCmd As New OracleCommand
        Private dbconn As New OracleConnection
    
        Private Sub Initialize()
            With selCmd
                .CommandText = "select * from tbl_users where 1 = 1"
                .CommandType = CommandType.Text
                .Connection = dbconn
            End With
    
            With insCmd
                .CommandText = "create_user"
                .CommandType = CommandType.StoredProcedure
                .Connection = dbconn
                .Parameters.Add("p_user_num", OracleType.Number).Direction = ParameterDirection.Output
                .Parameters.AddWithValue("p_username", Me.UsernameTextBox.Text)
                .Parameters.AddWithValue("p_password", Me.PasswordTextBox.Text)
                .Parameters.AddWithValue("p_title", Me.TitleTextBox.Text)
                .Parameters.AddWithValue("p_last_name", Me.Last_nameTextBox.Text)
                .Parameters.AddWithValue("p_first_name", Me.First_nameTextBox.Text)
                .Parameters.AddWithValue("p_middle_name", Me.Middle_nameTextBox.Text)
                .Parameters.AddWithValue("p_email_address", Me.Email_addressTextBox.Text)
                .Parameters.AddWithValue("p_gender", Me.GenderTextBox.Text)
                .Parameters.AddWithValue("p_date_of_birth", Me.Date_of_birthDateTimeTextBox.Text)
                .Parameters.AddWithValue("p_description", Me.DescriptionTextBox.Text)
            End With
    
            With da
                .SelectCommand = selCmd
                .InsertCommand = insCmd
            End With
    
        End Sub
    
        Private Sub frmUser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Initialize()
            Dim dbstring As New OracleConnectionStringBuilder
            With dbstring
                .DataSource = "xe"
                .UserID = "dev"
                .Password = "password"
            End With
            dbconn.ConnectionString = dbstring.ConnectionString
            dbconn.Open()
        End Sub
    
        Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
            Me.Validate()
            Me.Tbl_usersBindingSource.EndEdit()
            Me.da.Update(Me.DsUser.tbl_users)
        End Sub
    so i run the form, click the AddNew button in the BindingNavigator, enter the data in the textboxes and then click Save. i receive no error but the values of the textboxes are not being passed to my stored procedure. it is creating records but all columns are null.

    i checked this by using an SQL Editor to query the table back-end.

    can anyone help?

    thanks
    Last edited by adshocker; Mar 12th, 2009 at 01:36 AM.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] need help in using stored proc for insert command

    That would be because you're calling Initialize, and therefore setting the parameter values, before the user has entered any data. You have to wait until the user has entered data into the TextBoxes until you can get that data in code and use it to set the parameter values.

    Also, you should NOT be using AddWithValue with a DataAdapter. With a DataAdapter the parameter values are supposed to come from the DataTable, not be set explicitly by you. Take a look at this.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] need help in using stored proc for insert command

    hi,

    after reading your comments i changed my code to

    vb Code:
    1. Private Sub Initialize()
    2.         With selCmd
    3.             .CommandText = "select * from tbl_users where 1 = 1"
    4.             .CommandType = CommandType.Text
    5.             .Connection = dbconn
    6.         End With
    7.  
    8.         With insCmd
    9.             .CommandText = "create_user"
    10.             .CommandType = CommandType.StoredProcedure
    11.             .Connection = dbconn
    12.             .Parameters.Clear()
    13.             .Parameters.Add("p_user_num", OracleType.Number).Direction = ParameterDirection.Output
    14.             .Parameters.Add(":p_username", OracleType.VarChar, 30, "p_username")
    15.             .Parameters.Add(":p_password", OracleType.VarChar, 30, "p_password")
    16.             .Parameters.Add(":p_title", OracleType.VarChar, 10, "p_title")
    17.             .Parameters.Add(":p_last_name", OracleType.VarChar, 30, "p_last_name")
    18.             .Parameters.Add(":p_first_name", OracleType.VarChar, 30, "p_first_name")
    19.             .Parameters.Add(":p_middle_name", OracleType.VarChar, 30, "p_middle_name")
    20.             .Parameters.Add(":p_email_address", OracleType.VarChar, 100, "p_email_address")
    21.             .Parameters.Add(":p_gender", OracleType.VarChar, 1, "p_gender")
    22.             .Parameters.Add(":p_date_of_birth", OracleType.DateTime, 30, "p_date_of_birth")
    23.             .Parameters.Add(":p_description", OracleType.VarChar, 250, "p_description")
    24.  
    25.         End With
    26.  
    27.         With da
    28.             .SelectCommand = selCmd
    29.             .InsertCommand = insCmd
    30.         End With
    31.  
    32.     End Sub

    i also moved the sub Initialize after the opening the connection... not sure if thats what you wanted me to do...

    vb Code:
    1. Private Sub frmUser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.  
    3.         Dim dbstring As New OracleConnectionStringBuilder
    4.         With dbstring
    5.             .DataSource = "xe"
    6.             .UserID = "dev"
    7.             .Password = "password"
    8.         End With
    9.         dbconn.ConnectionString = dbstring.ConnectionString
    10.         dbconn.Open()
    11.  
    12.         Initialize()
    13.     End Sub

    i'm getting a different error now "PLS-00306: wrong number or types of arguments in call to 'create_user'".. the error seems to say that i'm not giving enough values..

    i think the problem might be the srcColumn i'm passing when adding the parameter... not sure what to put there.

    can someone help me?

    thanks.
    Last edited by adshocker; Mar 12th, 2009 at 04:30 PM.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] need help in using stored proc for insert command

    never mind.. i figured out what the problem was..

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width