Click here to Skip to main content
14,735,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Invalid object name sp_VehicleInfo.


Code:
protected void btnRegister_Click(object sender, EventArgs e)
{
        VehicleInfo vehicle = new VehicleInfo();
        vehicle.VehicleNo = txtVehicleNo.Text;
        vehicle.Make = txtMake.Text;
        vehicle.Model = txtEmail.Text;
        vehicle.Variety = txtVersion.Text;
        //vehicle.isAc = radAC.Text;
        vehicle.EngineNo = TextBox1.Text;
        vehicle.LExpDate = txtLicenseExpDate.Text;
        vehicle.InsuranceNo = TextBox2.Text;
        vehicle.IExpDate = txtInsuranceExpDate.Text;
        vehicle.Insert(); 
}




and this is Stored Procedure:

[dbo].[sp_VehicleInfo]              
    @VehicleNo NVARCHAR(50) = NULL,
    @Make NVARCHAR(50) = NULL,
    @Model NVARCHAR(50) = NULL,
    @Variety NVARCHAR(50) = NULL,
    @isAc BIT = NULL,
    @EngineNo NVARCHAR(50) = NULL,
    @LExpDate NVARCHAR(50) = NULL,
    @InsuranceNo NVARCHAR(50) = NULL,
    @IExpDate NVARCHAR(50) = NULL,
    @VehicleId bigint = NULL,
    @mode NVARCHAR(50)
AS
BEGIN
    IF(@mode = 'insert')
    BEGIN
        INSERT INTO sp_VehicleInfo (VehicleNo, Make, Model, Variety, isAc, EngineNo, LExpDate, InsuranceNo, IExpDate)
        OUTPUT inserted.VehicleNo
        VALUES (@VehicleNo, @Make, @Model, @Variety, @isAc, @EngineNo, @LExpDate, @InsuranceNo, @IExpDate)
    END

    IF(@mode = 'update')
    BEGIN
        UPDATE sp_VehicleInfo
        SET VehicleNo = Isnull(@VehicleNo, VehicleNo),
            Make = Isnull(@Make, Make),
            Model = Isnull(@Model, Model),
            Variety = Isnull(@Variety, Variety),
            isAc = Isnull(@isAc, isAc),
            EngineNo = Isnull(@EngineNo, EngineNo),
            LExpDate = Isnull(@LExpDate, LExpDate),
            InsuranceNo = Isnull(@InsuranceNo, InsuranceNo),
            IExpDate = Isnull(@IExpDate, IExpDate)
        WHERE 
            VehicleId = @VehicleId
    END

    IF(@mode = 'get' )
    BEGIN
        SELECT * 
        FROM sp_VehicleInfo 
        WHERE VehicleId = @VehicleId
    END

    IF(@mode = 'delete') 
    BEGIN
        DELETE FROM sp_VehicleInfo  
        WHERE VehicleId = @VehicleId
    END
END
Posted
Comments
_Asif_ 19-Jan-16 2:11am
   
How do vehicle.Insert() know that it has to call sp_VehicleInfo stored procedure? Where have you defined this? And there are problems in your SP as you are using the SP name in your INSERT INTO sp_VehicleInfo query

Look at the error message:
"Invalid object name sp_VehicleInfo."
That looks like the name of a stored procedure, not a table - so trying to UPDATE it will give you an error...
Check your names!
   
There is something missing in code, try using passing parameter to stored procedure while calling it, try this : cmd.Parameters.AddWithValue
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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