C# SqlException:数据类型tinyint的算术溢出错误,值=-1
我一直试图将一个值为1的int传递给我的存储过程,但我不断得到一个异常,即“数据类型tinyint的算术溢出错误,value=-1”。int的值为1,但异常显示为-1,我不明白为什么。无论如何,我已经张贴了下面的代码。我可能遗漏了一些明显的东西 .net代码C# SqlException:数据类型tinyint的算术溢出错误,值=-1,c#,asp.net,tsql,ado.net,C#,Asp.net,Tsql,Ado.net,我一直试图将一个值为1的int传递给我的存储过程,但我不断得到一个异常,即“数据类型tinyint的算术溢出错误,value=-1”。int的值为1,但异常显示为-1,我不明白为什么。无论如何,我已经张贴了下面的代码。我可能遗漏了一些明显的东西 .net代码 protected void btnUpdate_Click(object sender, EventArgs e) { string result = ""; string currentFileN
protected void btnUpdate_Click(object sender, EventArgs e)
{
string result = "";
string currentFileName = (string)(Session["FileName"]);
string FileName = txtUploadStatus.Text;
string sSQL = "usp_imageloader_update";
int ImgID = (int)(Session["ImgID"]);
using (SqlConnection dbConnection = new SqlConnection(CKS_app_settings.sql_conn_string_db))
{
// SqlTransaction tn=null;
try
{
dbConnection.Open();
//start Transaction
// tn = dbConnection.BeginTransaction();
SqlCommand command = new SqlCommand(sSQL, dbConnection);
//command.Transaction = tn;
command.CommandText = sSQL;
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 1024;
int Active = 0;
if (chkActive.Checked == true)
{
Active = 1;
}
else
{
}
if (currentFileName != FileName)
{
// Split entire file path to grab filename
string[] split = txtUploadStatus.Text.Split(new char[] { '\\' });
FileName = split[06];
}
else
{
FileName = txtUploadStatus.Text;
}
command.Parameters.AddWithValue("@p_image_id", ImgID);
command.Parameters.AddWithValue("@p_filename", FileName);
command.Parameters.AddWithValue("@p_Active", Active);
command.Parameters.AddWithValue("@p_url", txtUrl.Text);
command.Parameters.AddWithValue("@p_Title", txtImgTitle.Text);
command.Parameters.AddWithValue("@p_alt_text", txtAlt.Text);
int rowsAffected = command.ExecuteNonQuery();
}
catch (SqlException ex)
{
// throw ex;
//If it failed for whatever reason, rollback the //transaction
//tn.Rollback();
//No need to throw because we are at a top level call and //nothing is handling exceptions
result = ex.InnerException.Message;
}
USE [smsdb_test_griffin2]
GO
/****** Object: StoredProcedure [dbo].[usp_imageloader_update] Script Date: 01/09/2012 08:52:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[usp_imageloader_update]
@p_image_id INT,
@p_filename VARCHAR(100),
@p_Active TINYINT,
@p_url VARCHAR(255),
@p_Title VARCHAR(100),
@p_alt_text VARCHAR(255)
as
UPDATE Image_Library_UK
SET
Image_name=@p_filename,
Active=-@p_Active,
Url=@p_url,
Alt_text=@p_alt_text
WHERE Image_id=@p_image_id
存储过程
protected void btnUpdate_Click(object sender, EventArgs e)
{
string result = "";
string currentFileName = (string)(Session["FileName"]);
string FileName = txtUploadStatus.Text;
string sSQL = "usp_imageloader_update";
int ImgID = (int)(Session["ImgID"]);
using (SqlConnection dbConnection = new SqlConnection(CKS_app_settings.sql_conn_string_db))
{
// SqlTransaction tn=null;
try
{
dbConnection.Open();
//start Transaction
// tn = dbConnection.BeginTransaction();
SqlCommand command = new SqlCommand(sSQL, dbConnection);
//command.Transaction = tn;
command.CommandText = sSQL;
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 1024;
int Active = 0;
if (chkActive.Checked == true)
{
Active = 1;
}
else
{
}
if (currentFileName != FileName)
{
// Split entire file path to grab filename
string[] split = txtUploadStatus.Text.Split(new char[] { '\\' });
FileName = split[06];
}
else
{
FileName = txtUploadStatus.Text;
}
command.Parameters.AddWithValue("@p_image_id", ImgID);
command.Parameters.AddWithValue("@p_filename", FileName);
command.Parameters.AddWithValue("@p_Active", Active);
command.Parameters.AddWithValue("@p_url", txtUrl.Text);
command.Parameters.AddWithValue("@p_Title", txtImgTitle.Text);
command.Parameters.AddWithValue("@p_alt_text", txtAlt.Text);
int rowsAffected = command.ExecuteNonQuery();
}
catch (SqlException ex)
{
// throw ex;
//If it failed for whatever reason, rollback the //transaction
//tn.Rollback();
//No need to throw because we are at a top level call and //nothing is handling exceptions
result = ex.InnerException.Message;
}
USE [smsdb_test_griffin2]
GO
/****** Object: StoredProcedure [dbo].[usp_imageloader_update] Script Date: 01/09/2012 08:52:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[usp_imageloader_update]
@p_image_id INT,
@p_filename VARCHAR(100),
@p_Active TINYINT,
@p_url VARCHAR(255),
@p_Title VARCHAR(100),
@p_alt_text VARCHAR(255)
as
UPDATE Image_Library_UK
SET
Image_name=@p_filename,
Active=-@p_Active,
Url=@p_url,
Alt_text=@p_alt_text
WHERE Image_id=@p_image_id
在update语句中,在调用@p_active之前有一个减号: 活动=-@p_活动
可能是你的问题 在将值赋给Active时,存储过程中@p_Active前面有一个负号
Active=-@p\u Active,