Results 1 to 10 of 10

Thread: [2005] Unable to insert data to database using stored procedures

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    150

    [2005] Unable to insert data to database using stored procedures

    After executing this code , I get no errors , but nothing is added to the database !


    Code:
        
    
            Dim MyCmd As SqlCommand = New SqlCommand()
    
            MyCmd.CommandText = "insNewComplain"
            MyCmd.CommandType = Data.CommandType.StoredProcedure
    
    
            MyCmd.Parameters.Clear()
            MyCmd.Parameters.Add("@TypeID", SqlDbType.Int)
            MyCmd.Parameters.Add("@LocationID", SqlDbType.Int)
            MyCmd.Parameters.Add("@MobileNO", SqlDbType.Char)
            MyCmd.Parameters.Add("@CallerNO", SqlDbType.Char)
            MyCmd.Parameters.Add("@CallerName", SqlDbType.Char)
            MyCmd.Parameters.Add("@ComplainDetails", SqlDbType.Text)
            MyCmd.Parameters.Add("@ImportanceID", SqlDbType.Int)
            MyCmd.Parameters.Add("@DeathID", SqlDbType.Int)
            MyCmd.Parameters.Add("@StreetID", SqlDbType.Int)
            '-----------------------------------------------------------
    
            MyCmd.Parameters("@LocationID").Value = LocationsComboBox.SelectedValue
            MyCmd.Parameters("@TypeID").Value = ComplainTypeComboBox.SelectedValue
            MyCmd.Parameters("@MobileNO").Value = txtCallerMobile.Text
            MyCmd.Parameters("@CallerNO").Value = txtCallerNO.Text
            MyCmd.Parameters("@CallerName").Value = txtCallerName.Text
            MyCmd.Parameters("@ComplainDetails").Value = txtComplainDetails.Text
            MyCmd.Parameters("@ImportanceID").Value = ImportanceComboBox.SelectedValue
            MyCmd.Parameters("@DeathID").Value = 1 'HERE YOU HAVE TO STORE THE RADIO BUTTON OPTION 
            MyCmd.Parameters("@StreetID").Value = StreetsComboBox.SelectedValue
    
    
            MyConn = New SqlConnection
    
    
     
            MyCmd.Connection = MyConn
    
    
                MyConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MYDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
                MyConn.Open()
    
                    MyCmd.ExecuteNonQuery()
    
                Catch
                    MessageBox.Show("Unable to Execute SQL " & Err.Description)
                    Exit Sub
                End Try
    
            Catch
                Exit Sub
            End Try
    
    
    
            MessageBox.Show("INSERT WAS SUCCESSFUL!")
    
    
            MyConn.Close()
    
    
            MyCmd = Nothing

    and here is my stored procedure:

    Code:
    ALTER PROCEDURE dbo.insNewComplain 
    	
    	@TypeID int ,
    	@LocationID int,
    	@MobileNO char(10),
    	@CallerNO char(10),
    	@CallerName varchar(50),
    	@ComplainDetails text,
    	@ImportanceID int,
    	@DeathID int,
    	@StreetID int
    	
    AS
    
    
    	INSERT INTO Complains(TypeID, LocationID, MobileNO, CallerNO, CallerName, ComplainDate, ComplainDetails,
    	ImportanceID, DeathID, StreetID)
    	
    	VALUES (@TypeID, @LocationID, @MobileNO, @CallerNO, @CallerName, getDate (),@ComplainDetails, 
    	@ImportanceID, @DeathID, @StreetID)

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

    Re: [2005] Unable to insert data to database using stored procedures

    What value does ExecuteNonQuery return?
    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
    Addicted Member
    Join Date
    Aug 2005
    Posts
    150

    Re: [2005] Unable to insert data to database using stored procedures

    It doesn't return anything ?

    I'm not sure , how do I check , must the ExecuteNonQuery return something ?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Unable to insert data to database using stored procedures

    ExecuteNonQuery returns the number of rows affected. So if it returns 1, one row was inserted. If it returns 0, no rows were inserted.

    Now, I see from your connectionstring you are using user instance of SQL Server for the database.... How are you checking that the data isn't in the database? If you are checking the mdf that is being pointed to in the connection string, then it won't be there. It's only used as a template for the mdf database file that gets copied to your user folder. May want to look in there.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: [2005] Unable to insert data to database using stored procedures

    Of course it returns something. It's a function. All functions return something. What does it return?
    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

  6. #6
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] Unable to insert data to database using stored procedures

    Debug the code is the best way and see are you getting any error!
    Also it's good that after making the stored procedure execute it manually first in the SQL SERVER also


    Rather than .Add use the .AddWithValue in this way

    VB.NET Code:
    1. MyCmd.Parameters.AddWithValue("@LocationID",LocationsComboBox.SelectedValue)
    2.         MyCmd.Parameters.AddWithValue("@MobileNO", txtCallerMobile.Text)
    3. 'And so on...
    Last edited by shakti5385; Sep 10th, 2007 at 03:48 AM.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Unable to insert data to database using stored procedures

    Personally I think the .Add is preferable over .AddWithValue
    1) I don't have to specify a value. But then, I usually do a lot of looping, so adding parameters w/o values makes sense to me.
    2) I can specify the datatype explicitly, rather than having it inferred.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] Unable to insert data to database using stored procedures

    Quote Originally Posted by techgnome
    Personally I think the .Add is preferable over .AddWithValue
    1) I don't have to specify a value. But then, I usually do a lot of looping, so adding parameters w/o values makes sense to me.
    2) I can specify the datatype explicitly, rather than having it inferred.

    -tg
    May Be

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Unable to insert data to database using stored procedures

    Quote Originally Posted by jmcilhinney from the quoted thread
    AddWithValue is better where it's approrpate, otherwise Add is better.
    don't take it out of context... it wasn't blindly stated that AddWithValue is always better.... but where it's APPROPRIATE. And as in the case of a loop, I don't think it is....

    But you are entitled to your opinion...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: [2005] Unable to insert data to database using stored procedures

    AddWithValue should be used where you are executing a one-off command. You add the parameters and values once and then discard them. If you are going to use the command more than once and will/may need to change the parameter values then you should use Add.
    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

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