Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a stored procedure that takes several parameters and my aim is to insert these values into a table. My table has an auto increment field as {int identity(1,1)}, I tried to insert data as there is no such a column since on internet there were some examples like that. Although there is no error and try catch block does not catch anything, after succesful finishing of program, data is not inserted. Why??? help please

here is my table:

SQL
mytable([productcode] [varchar](100) NULL,
	[productid] [varchar](30) NULL,
	[mydate] [smalldatetime] NULL,
	[Id] [int] IDENTITY(1,1) NOT NULL
)


here is my sp:
SQL
procedure mysp( @procode varchar(100),@pid varchar(30),@mydate smalldatetime)
as begin
insert into mytable(productcode,productid,mydate) values(@procode,@pid,@mydate)
end


here is my code:

C#
#region Database
Database db = DatabaseFactory.CreateDatabase("sample.Properties.Settings.mydbConnectionString");
DbCommand cmd = db.GetStoredProcCommand("mysp");
db.AddInParameter(cmd, "@procode", DbType.String, pcode);
db.AddInParameter(cmd, "@pid", DbType.String, pid);
db.AddInParameter(cmd, "@mydate", DbType.DateTime, dt);
#endregion
try{  
   db.ExecuteDataSet(cmd);
}
catch (Exception ex){
   Console.WriteLine(ex.Message);
}



Thanks in advance
Posted
Updated 22-Oct-11 14:41pm
v3
Comments
Maciej Los 22-Oct-11 15:38pm    
If you want to take help from us, we need to know the structure of the table and the code to insert data.
Amir Mahfoozi 23-Oct-11 1:59am    
The stored procedure looks fine, so please run SQL Profiler to make sure that stored procedure is called with correct parameter values. If it's not called by correct parameter values then review your code and see where it should fill parameters or where it clears! parameter values.

1 solution

As far as I know the auto increment field can be ignored.
So lets assume you have a table looking like this:
ID(int) <- increment
Name(varchar(50))

You could write an insert that looks like this:
SQL
insert into MyTable
(Name)
values
('Naerling')

This will successfully insert a record into your table with the Name Naerling. The ID is automatically incremented and added.
Hope that helps.

Edit: In the above example you do not even have to specify the tables, since there is only one (besides the ID) which is filled. So the following would also work:
SQL
insert into MyTable
values ('Naerling')
 
Share this answer
 
v2
Comments
kurtiniadiss 22-Oct-11 15:39pm    
I did it exactly with this way, no errors no warnings but it is not added. It is too frustrating:)
Sander Rossel 22-Oct-11 15:58pm    
Could you post your query/table structure?
If the method I just described does not work I assume an error occurs, but there is no way any of us can tell without your table/query.
kurtiniadiss 22-Oct-11 16:14pm    
I updated my question, added table and sp structure
Sander Rossel 22-Oct-11 20:46pm    
Thanks. I added pre tags for readability.
The bad news is I can't find anything that is obviously wrong... Have you debugged? Check the values of the variables you put in the cmd.AddInParameter. So pcode, pid and dt. Also, does db.ExecuteDataSet return a result or anything? I can't see anything wrong on the database side. I suspect it is somehow in your code.
kurtiniadiss 23-Oct-11 3:55am    
Yes I debugged it many times, parameters are true and all steps are being done succesfully,but there is no row added into my table. I implemented delete and update by same way and they are working fine. I am wondering about that is there any special trick about insertion process. sp also work succesfully when I run it on the SSMS side.

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