Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two tables artist and arts. artistId is a primary key in artist table and it is the foreign key in arts table.I am using stored procedure to insert data. first artist log in and then art info page is displayed where artist insert art info.I want that artist id to get inserted in arts table also when he inserts art info.I tried this as:
stored procedure:
if exists(select artistId from artist)
insert into arts(name,description,artistId) values(@name,@description,@artistId)

asp.net code:

SQL
cmd.Parameters.AddWithValue("@name",txtname.Text);
        cmd.Parameters.AddWithValue("@category",ddlcategory.SelectedValue);

It is not working.
I am new to stored procedure.Please help me to get the foreign key value.Or is there any other way?
Posted
Comments
CHill60 28-Apr-14 10:54am    
What do you mean by "not working" - please be specific
Member 10724668 28-Apr-14 11:00am    
it is giving error as artistId can not be null
DamithSL 28-Apr-14 11:52am    
can you update the question with full stored procedure code and full insert C# code?

1 solution

you have 3 parameters in your stored procedure, need to set all the parameter values.
when artist login to system you can get the artistId from the database and keep that value.

C#
cmd.Parameters.AddWithValue("@name",txtname.Text);
cmd.Parameters.AddWithValue("@category",ddlcategory.SelectedValue);
cmd.Parameters.AddWithValue("@artistId",artistId);
 
Share this answer
 
v2
Comments
Member 10724668 28-Apr-14 11:13am    
It is giving error as the name artistId does not exist in the current context
DamithSL 28-Apr-14 11:33am    
that's why I added "when artist login to system you can get the artistId from the database and keep that value"
DamithSL 28-Apr-14 11:49am    
is this windows form application or web application?
Member 10724668 28-Apr-14 12:01pm    
web application

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