Search code examples
asp.netsql-server-2008datatableinsertion

Getting error while inserting table in database?


i am inserting table in database using table datatype with the following code:

CREATE TYPE BackUpDoctorLocationAreaRoom AS TABLE (
[RoomId] bigint,
[AreaId] bigint,
[LocationId] bigint
);

Alter proc proc_tblBackUpDoctorInsert 
(
@Id uniqueidentifier='',
@BackUpDoctorId uniqueidentifier='1323e1f4-7a93-4b45-9a9b-3840c32fd6d8',  
@StartDate datetime='11/08/2012',  
@EndDate datetime='11/09/2012',  
@StartTime datetime='22:22:22',  
@EndTime datetime='01:11:11',  
@CreatedBy uniqueidentifier='acf7961c-4111-49ad-a66a-ce7f9ce131bd',  
@ModifiedBy uniqueidentifier='acf7961c-4111-49ad-a66a-ce7f9ce131bd',  
@createdDate datetime='11/6/12 3:09:58 AM',  
@ModifiedDate datetime='11/6/12 3:09:58 AM',
@tblBackUpDoctorsForRooms BackUpDoctorLocationAreaRoom READONLY
)    
as    
set xact_abort on
declare @newId uniqueidentifier;    
set @newId = newid(); 
insert into tblBackUpDoctor (Id,BackUpDoctorId,StartDate,EndDate,StartTime,EndTime,CreatedBy,ModifiedBy,
createdDate,ModifiedDate,IsActive,isdeleted) values 
(@newId,    
@BackUpDoctorId,    
@StartDate,    
@EndDate,    
@StartTime,    
@EndTime,    
@CreatedBy,    
@ModifiedBy,    
@createdDate,    
@ModifiedDate,    
1,0)    
declare @IdFortblBackUpDoctorsForRooms uniqueidentifier;    
set @IdFortblBackUpDoctorsForRooms = newid();    
delete from tblBackUpDoctorsForRooms where BackUpRecordId=@id and Roomid in (Select roomid from @tblBackUpDoctorsForRooms)
delete from tblbackupdoctor where id=@id
insert into tblBackUpDoctorsForRooms (BackUpRecordId,Roomid,Araeid,locationid)    
Select  @newId,roomid,areaid,locationid from @tblBackUpDoctorsForRooms
select @newId

This is the sp in which i am using that table.

My class file's code is :

public string InsertBackUpDoctor(ClsBackUpDoctorProp objProp, DataTable dtLocAreaRoom)
{
    String ConnectionString = CCMMUtility.GetCacheForWholeApplication();
    String backUpRecordId = "";
    SqlParameter[] param = new SqlParameter[12];
    param[0] = new SqlParameter("@Id", objProp.Id);
    param[1] = new SqlParameter("@BackUpDoctorId", objProp.BackUpDoctorId);
    param[2] = new SqlParameter("@StartDate", objProp.StartDate);
    param[3] = new SqlParameter("@EndDate", objProp.EndDate);
    param[4] = new SqlParameter("@StartTime", objProp.StartTime);
    param[5] = new SqlParameter("@EndTime", objProp.EndTime);
    param[6] = new SqlParameter("@CreatedBy", objProp.CreatedBy);
    param[7] = new SqlParameter("@ModifiedBy", objProp.ModifiedBy);
    param[8] = new SqlParameter("@createdDate", CCMMUtility.GetCurrentDateTimeByTimeZone("US Mountain Standard Time"));
    param[9] = new SqlParameter("@ModifiedDate", CCMMUtility.GetCurrentDateTimeByTimeZone("US Mountain Standard Time"));
    param[10] = new SqlParameter("@CurrentDate", objProp.CurrentDate);
    param[11] = new SqlParameter("@tblBackUpDoctorsForRooms ", dtLocAreaRoom);


    backUpRecordId = SqlHelper.ExecuteScalar(ConnectionString, "proc_tblbackupdoctorInsertBackUpDoctors", param).ToString();
    return backUpRecordId;
}

and here is the error which is coming when i tries to insert :

The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Table-valued parameter 12 ("@tblBackUpDoctorsForRooms"), row 0, column 0: Data type 0xF3 (user-defined table type) has a non-zero length database name specified. Database name is not allowed with a table-valued parameter, only schema name and type name are valid. I dont know why this coming please help me..


Solution

  • I believe you'd have to change the way you pass your custom parameter:

    Not just

    param[11] = new SqlParameter("@tblBackUpDoctorsForRooms ", dtLocAreaRoom);
    

    but rather something like

    SqlParameter parameter = new SqlParameter();           
    
    parameter.ParameterName = "@tblBackUpDoctorsForRooms";               
    parameter.SqlDbType = System.Data.SqlDbType.Structured;
    parameter.TypeName = "BackUpDoctorLocationAreaRoom";                
    parameter.Value = dtLocAreaRoom;                                  
    
    param[11] = parameter;