Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL query Stored
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:
 
mytable([productcode] [varchar](100) NULL,
	[productid] [varchar](30) NULL,
	[mydate] [smalldatetime] NULL,
	[Id] [int] IDENTITY(1,1) NOT NULL
)
 
here is my sp:
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:
 
#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 22-Oct-11 9:26am
Edited 22-Oct-11 14:41pm
v3
Comments
losmac at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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:
insert into MyTable
values ('Naerling')
  Permalink  
v2
Comments
kurtiniadiss at 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:)
Naerling at 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 at 22-Oct-11 16:14pm
   
I updated my question, added table and sp structure
Naerling at 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 at 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)

  Print Answers RSS
0 OriginalGriff 6,696
1 Sergey Alexandrovich Kryukov 6,675
2 CPallini 5,315
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 23 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100