Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all
i have one small assignment in windows apps with sql database.
I have created two tables one with ID,FN, LN and second one with LocationID, city, state, ID.
I have also created relationship between this two tables on ID
and assignment is all about stored procedure so we have to use sp to insert data
this is my sp
ALTER PROCEDURE dbo.NewInsertCommand
(
	@FirstName nvarchar(25),
	@LastName nvarchar(25),
	@City nvarchar(50),
	@State nvarchar(50)
)
AS
	SET NOCOUNT OFF;
	DECLARE @UserID int;
	
INSERT INTO [UserData] ([FirstName], [LastName]) VALUES (@FirstName, @LastName);
	
SELECT @UserID=@@IDENTITY;
INSERT INTO [Location] (City,State,UserID)
VALUES (@City, @State, @UserID);

and bellow this is my vb for button click event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        SqlCommand1 = New SqlCommand("NewInsertCommand", SqlConnection1)
        SqlCommand1.CommandType = CommandType.StoredProcedure
        SqlCommand1.CommandText = "NewInsertCommand"
        SqlCommand1.Parameters.AddWithValue("FirstName", FirstName.Text)
        SqlCommand1.Parameters.AddWithValue("LastName", LastName.Text)
        SqlCommand1.Parameters.AddWithValue("City", ComboBox1.SelectedItem.ToString)
        SqlCommand1.Parameters.AddWithValue("State", ComboBox2.SelectedItem.ToString)

        SqlConnection1.Open()
        SqlCommand1.ExecuteNonQuery()
        SqlConnection1.Close()
        MsgBox("Item saved")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try


so now problem is when ever i try to insert data in to database it dosen't store in to it but if i right click in stored procedure file (dbo.NewInsertCommand) and select step into and input all the parameters in run sp dialog box then it's saves data into database
any help please

thanks again
Posted
Updated 4-Jul-10 5:26am
v2
Comments
Sandeep Mewara 4-Jul-10 6:51am    
Have you used DEBUGGER and checked the code part is running fine and no error is there?
bapu2889 4-Jul-10 7:09am    
hello thanks for your rep.
yes but to be very honest i dont know what to check.
but it's taking all the strings from textbox and then run ExecuteNonQuery and it displays item saved message box so could you please tell me what should i check
waiting for your kind help
thanks

Ok.

Looks like the way you have added the parameters to the SP are not correct. Parameter names are not properly passed. Stored Procedures won't stop if there is an error.

C#
SqlCommand1.Parameters.AddWithValue("FirstName", FirstName.Text)

is not the right way to add the parameters.

Try:
C#
SqlParameter objParam1 = new SqlParameter();
objParam1.ParameterName = "@FirstName";
objParam1.Value = "Sandeep";
SqlCommand1.Parameters.Add(objParam1);



Your SP is fine. You need to make changes in your code.
If needed, for more details have a look here:
SqlCommand.Parameters [^]
 
Share this answer
 
Comments
bapu2889 5-Jul-10 11:28am    
hi sandeep
yes i have tried but still it's same when i run the apps it runs fine and also shows saved data in to listview because in this app i also have listview control to retrive data from database. so when i saved data it shows into listview contorl but when i check into database nothing shows there
thanks for your rep.
Use SQL Profiler to see what calls are being made and what values are being sent to your stored proc or add some trace type statements to help understand what is happening while not debugging.

Two other things, unrelated to your problem

SET NOCOUNT ON rather than SET NOCOUNT OFF

Use SCOPE_IDENTITY rather than @@IDENTITY
 
Share this answer
 
Comments
bapu2889 4-Jul-10 12:31pm    
hello mark
thanks for your rep.
could you please tell me how to use scope_identity
waiting for your kind rep.

thanks
bapu2889 4-Jul-10 12:42pm    
hello again
i have also tried this

ALTER PROCEDURE dbo.NewInsertCommand
(
@FirstName nvarchar(25),
@LastName nvarchar(25),
@City nvarchar(50),
@State nvarchar(50)

)
AS
SET NOCOUNT ON;
DECLARE @UserID int;

INSERT INTO [UserData] ([FirstName], [LastName]) VALUES (@FirstName, @LastName);

SET @UserID = SCOPE_IDENTITY();


INSERT INTO [Location] (City,State,UserID)
VALUES (@City, @State, @UserID);

but still no luck
Hello all
thanks every one who has posted for my question.
this is what i have done and now it's working fine without any problem all 4 operation insert, delete, update, and search with stored procedure.
but only one thing i dont understand when i started this assignment i just drag and drop sqlcommand and sqlconnection but it was not working and then i have removed those two and add this


VB
Private Sub InsertData()

<pre>    Try
        Dim Con As New SqlConnection
        Con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & MyString & "\TrialDataBase.mdf;Integrated Security=True;User Instance=True"
        Dim cmd As New SqlCommand("NewInsertCommand", Con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = FirstName.Text
        cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = LastName.Text
        cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = ComboBox1.SelectedItem.ToString
        cmd.Parameters.Add("@State", SqlDbType.NVarChar).Value = ComboBox2.SelectedItem.ToString

        Con.Open()
        cmd.ExecuteNonQuery()
        MsgBox("Data Saved Successfully !", MsgBoxStyle.Information)
        Con.Close()
        FirstName.Clear()
        LastName.Clear()
    Catch ex As SqlException
        MsgBox(ex, MsgBoxStyle.Critical)
    End Try

End Sub



but anyway it's done and dusted but i still want to know why drag and drop dosen't work
thanks again
have a nice day
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900