简体   繁体   中英

Operand type clash: int is incompatible with DaysAndHours

Exception: Operand type clash: int is incompatible with DaysAndHours at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action

The above exception is getting when I add int type value to SQL parameters.How to pass the int value to sql parameters.

C# code:

 public bool VendorDaysAndHoursInfoByMemberID(UpdateVendaorDaysAndHoursDM updateVendaorDaysAndHoursDM)
        {
            _logger.LogInformation("VendorRepository.vendorDaysAndHoursInfoByMemberID method called!!!");
            var memberId = updateVendaorDaysAndHoursDM.MemberID;

            var vendorDaysAndHoursDMsList = updateVendaorDaysAndHoursDM.DaysAndHours.Select(c => c).Select(c => c).ToList();
            DataTable dtVendorDaysAndHoursDMsList = DataReaderToList.ToDataTable<DaysAndHoursDM>(vendorDaysAndHoursDMsList);

            using (SqlConnection conn = new SqlConnection(GetComapnyConnection.ConnectionString))
            {
                conn.Open();
                using (var transaction = conn.BeginTransaction())
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("exec VendorDaysAndHoursInfoByMemberID @MemberID, @DaysAndHours", conn, transaction);

                        cmd.CommandTimeout = 0;
                        cmd.Transaction = transaction;

                       cmd.Parameters.AddWithValue("@MemberID", SqlDbType.Int).Value = memberId;

                        //cmd.Parameters.Add(new SqlParameter()
                        //{
                        //    ParameterName = "@MemberID",
                        //    SqlDbType = SqlDbType.Structured,
                        //    Value = memberId,
                        //    TypeName = "MemberID"
                        //});

                        var pDaysAndHoursDMsList = new SqlParameter("@DaysAndHours", SqlDbType.Structured);
                        pDaysAndHoursDMsList.TypeName = "DaysAndHours";
                        if (dtVendorDaysAndHoursDMsList.Rows.Count == 0)
                            pDaysAndHoursDMsList.Value = null;
                        else
                            pDaysAndHoursDMsList.Value = dtVendorDaysAndHoursDMsList;
                        cmd.Parameters.Add(pDaysAndHoursDMsList);
                        var dataReader = cmd.ExecuteReader();
                        var isResult = DataReaderToList.DataReaderMapToList<bool>(dataReader);

                        dataReader.Dispose();

                        transaction.Commit();

                        conn.Close();

                        return true;
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        conn.Close();
                        _logger.LogError(e.Message);
                        return false;
                    }
                }
            }

        }

I need to post data in the below format.

{ 
"MemberID":"a7f1819db38ccb5e03ad52396eafa5ac"                    ,
"DaysAndHours":[
                    {"DayName":"Sunday","StartTime":"08:00:00","EndTime":"17:00:00","Available":false},
                    {"DayName":"Monday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
                    {"DayName":"Tuesday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
                    {"DayName":"Wednesday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
                    {"DayName":"Thursday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
                    {"DayName":"Friday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
                    {"DayName":"Saturday","StartTime":"08:00:00","EndTime":"19:00:00","Available":true}
                   ]
    }

How to pass the int value to SQL parameters in above format.

Stored Procedure:

Create proc [dbo].[VendorDaysAndHoursInfoByMemberID]
(
@DaysAndHours as [dbo].DaysAndHours readonly,
@MemberID int,
@IsSuccess bit= 'false' output
)
as begin
SET NOCOUNT ON;

    update DaysAndHours  set
    DaysAndHours.DayName=DaysAndHoursData.DayName,
    DaysAndHours.StartTime=DaysAndHoursData.StartTime,
    DaysAndHours.EndTime=DaysAndHoursData.EndTime,
    DaysAndHours.Available=DaysAndHoursData.Available

    from  @DaysAndHours DaysAndHoursData
 where MemberID = @MemberID;

 set @IsSuccess = 'true' 

End    
return @IsSuccess 

Code is not displaying errors.But isResult is getting count zero which is below of DataReader.

在此处输入图片说明

Sql Command is not properly configured. Try this


SqlCommand cmd = new SqlCommand("VendorDaysAndHoursInfoByMemberID", conn, transaction);

Stored Procedure code

Create proc [dbo].[VendorDaysAndHoursInfoByMemberID]
(
@DaysAndHours as [dbo].DaysAndHours readonly,
@MemberID int,
@IsSuccess bit= 0 output
)
as begin
SET NOCOUNT ON;

    update DaysAndHours  set
        DayName=@DaysAndHours.DayName,
        StartTime=@DaysAndHours.StartTime,
        EndTime=@DaysAndHours.EndTime,
        Available=@DaysAndHours.Available
from DaysAndHours join @DaysAndHours
ON DaysAndHours.MemberID =@DaysAndHours.MemberID 
 where MemberID = @MemberID;

 set @IsSuccess = 1 

End    
return @IsSuccess 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM