C#类型转换错误从SqlDatatype.BIT到C#中的布尔值 [英] C# Type Conversion error from SqlDatatype.BIT to Boolean in C#
问题描述
大家好
我得到< g class =" gr_ gr_87 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace"数据-GR-ID =" 87" id =" 87"> a a< / g>此行上的类型转换错误
SqlParameter参数= oCmd.SelectCommand.Parameters.Add(" @ blnSelectStatus",SqlDbType.Bit).Value = blnSelectByIDStatus .;
下面给出了代码片段。
请帮忙。
提前致谢。
public bool displayEventGalleryDetailsByGalleryByID(int ipknEventGalleryID)
{
$
bool blnSelectByIDStatus = false;
串zpConnectionString = WebConfigurationManager.ConnectionStrings [" MSC-UFCFUD-60-M"] ConnectionString.ToString();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; SqlConnection oCon = new SqlConnection(zpConnectionString);
SqlDataAdapter oCmd = new SqlDataAdapter();
$
oCmd.SelectCommand = new SqlCommand(" usp_EventGallery_Image_By_Image_Id_Select",oCon);
oCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
oCmd.SelectCommand.Parameters.Add(QUOT; @ ipknImageID" ;, SqlDbType.Int)。价值= ipknEventGalleryID;
&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //设置返回值的参数
SqlParameter参数= oCmd.SelectCommand.Parameters.Add(" @ blnSelectStatus",SqlDbType.Bit).Value = blnSelectByIDStatus .;
parameter.Direction = ParameterDirection.ReturnValue;
$
$
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;试试
{
DataSet oDs = new DataSet();
oCmd.Fill(oDs);
$
;&NBSP; //检索ReturnValue。
Int32 rowCount =(Int32)oCmd.SelectCommand.Parameters [" @ blnSelectStatus"]。Value;
$
  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;如果((int)的oCmd.SelectCommand.Parameters [" @ blnSelectStatus"]。值及0)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; blnSelectByIDStatus = true;
}
}
catch(例外情况)
{
抛出新的ApplicationException("数据错误。");
}
最后是
{
oCmd.Dispose();&
oCon.Close();
}
返回blnSelectByIDStatus;
$
}
}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
&NBSP;&NBSP;
该位为0 =真,1 =假就是这样的。所以你必须解释bool变量如果为true,你设置变量= 0和假设置变量= 1,你给出数字而不是bool值。
< span style ="color:#666666"> SqlParameter parameter = oCmd.SelectCommand.Parameters.Add(" @ blnSelectStatus",SqlDbType.Bit).Value = numericvalue;
Hi All
I am getting <g class="gr_ gr_87 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="87" id="87">an a</g> type conversion error on this line
SqlParameter parameter = oCmd.SelectCommand.Parameters.Add("@blnSelectStatus", SqlDbType.Bit).Value = blnSelectByIDStatus.;
The code snippet is given below.
Please help.
Thanks in advance.
public bool displayEventGalleryDetailsByGalleryByID(int ipknEventGalleryID)
{
bool blnSelectByIDStatus = false;
string zpConnectionString = WebConfigurationManager.ConnectionStrings["MSC-UFCFUD-60-M"].ConnectionString.ToString();
SqlConnection oCon = new SqlConnection(zpConnectionString);
SqlDataAdapter oCmd = new SqlDataAdapter();
oCmd.SelectCommand = new SqlCommand("usp_EventGallery_Image_By_Image_Id_Select", oCon);
oCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
oCmd.SelectCommand.Parameters.Add("@ipknImageID", SqlDbType.Int).Value = ipknEventGalleryID;
//Set the parameter for the return value
SqlParameter parameter = oCmd.SelectCommand.Parameters.Add("@blnSelectStatus", SqlDbType.Bit).Value = blnSelectByIDStatus.;
parameter.Direction = ParameterDirection.ReturnValue;
try
{
DataSet oDs = new DataSet();
oCmd.Fill(oDs);
// Retrieve the ReturnValue.
Int32 rowCount = (Int32)oCmd.SelectCommand.Parameters["@blnSelectStatus"].Value;
if ((int)oCmd.SelectCommand.Parameters["@blnSelectStatus"].Value > 0)
{
blnSelectByIDStatus = true;
}
}
catch (Exception ex)
{
throw new ApplicationException("Data error.");
}
finally
{
oCmd.Dispose();
oCon.Close();
}
return blnSelectByIDStatus;
}
}
The bit is 0 = true and 1 = false it's something like that. So you would have to interpret the bool variable if true you set a variable = 0 and false set variable = 1, and you give the number and not the bool value.
SqlParameter parameter = oCmd.SelectCommand.Parameters.Add("@blnSelectStatus", SqlDbType.Bit).Value = numericvalue;
这篇关于C#类型转换错误从SqlDatatype.BIT到C#中的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!