Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
@user_id int,
@journey_place varchar(50),
@pickup_date datetime,
@return_date datetime,
@pickup_address varchar(250),
@return_address varchar(250),
@service_id int,
@vehicle_id int,
@mode_of_payment varchar(50)

AS
    Begin

    Declare @uid int
    if(@uid = 0)
    begin
    set @uid = (select max(user_id) from registration)
    end

    Else
    begin
    set @uid = @user_id
    end


    insert into booking (user_id,journey_place,pickup_date,pickup_address,return_date,return_address,service_id,vehicle_id,mode_of_payment,status,starting_km,ending_km,booking_date)
     values (@user_id,@journey_place,@pickup_date,@pickup_address,@return_date,@return_address,@service_id,@vehicle_id,@mode_of_payment,'true',0,0,getdate())

    End



can any body tell me what should i pass argument in code to get userid..

cmd.Parameters.AddWithValue("@uid", ddluname.SelectedValue);
or

cmd.Parameters.AddWithValue("@user_id", ddluname.SelectedValue);

bt dis are not working.. and what should i pass argument in stored procedure.
Posted

The second one - @user_id.
The first one - @uid - is not in the parameters list.

Check the value you are passing - make sure it is an integer, or there may be problems.
If that doesn't work (or show up a problem) then we would need to see the error message, and the code that generates the SP request.
 
Share this answer
 
Comments
Member 9671810 17-Mar-13 10:36am    
first of all tell me is my insert query ok..?
Member 9671810 17-Mar-13 10:37am    
d thing is dat when i pass values and when user_id = 0 then there is confict in table.. of primary key and foregin key
frostcox 17-Mar-13 10:41am    
Your user_id cannot be 0 as there must be a foreign key constraint between the 2 tables
OriginalGriff 17-Mar-13 10:54am    
If you have a foreign key set up between this table and another using the user_id column, then you cannot create a record in thi stable unless the user_id matches with the value in the other table already. So if you are creating a booking for a user, the user must exist before the booking is created - which is correct if you think about it, because otherwise the bookings table would have invalid entries until you got around to creating the user. Assuming you ever did!

This means that you do not have a user with the ID of zero (which again, makes sense).
Create your user first, or retrieve the actual ID and use that.
Member 9671810 17-Mar-13 13:24pm    
ya.. bt i have to do both register and book.. and when i do both.. this don't work.. if i do only booking then it is booked.. bt what should i write to register and book
Hey you don't need to pass the @uid as you are setting it in the stored procedure.
You need to declare your @user_id parameter like so...
cmd.Paramaters.Add(new Sqlparamater("@user_id" , SqlDbType.Int)).Value = Convert.ToInt32(ddluname.selectedValue);
 
Share this answer
 
Comments
Member 9671810 17-Mar-13 10:41am    
is my insert query ok?
frostcox 17-Mar-13 10:44am    
The query looks fine can you show us the code from which you call the stored procedure?
OriginalGriff 17-Mar-13 10:44am    
AddWithValue is the shorter, cleaner, preferred option which does the same as the code you use.
frostcox 17-Mar-13 10:45am    
Cool always nice to get a good bit of advice every now and again, cheers.
write

VB
Declare @uid int
if(@user_id = 0)



instead of

VB
Declare @uid int
if(@uid = 0)
 
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