将nvarchar值'41/3/24'转换为数据类型int时,转换失败. [英] Conversion failed when converting the nvarchar value '41/3/24' to data type int.
本文介绍了将nvarchar值'41/3/24'转换为数据类型int时,转换失败.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的C#代码出现此错误
字符串 Patient_name = NameTxtBx.Text,Export_TO = ToTxtBx0.Text;
int PatNoVal;
PatNoVal = Convert.ToInt32(PatNo.Text);
PatNoVal = int .Parse(PatNo.Text);
十进制 PatID = 十进制 .Parse(PatID_NO.Text);
尝试
{
con.Open();
SqlCommand cmd = 新 SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = " ;
cmd.Parameters.AddWithValue(" ,Patient_name);
cmd.Parameters.AddWithValue(" ,Export_TO);
cmd.Parameters.AddWithValue(" ,DropDownList1.SelectedItem.Text);
cmd.Parameters.AddWithValue(" ,Label1.Text =会话[ 名称"].ToString());
cmd.Parameters.AddWithValue(" ,DropDownList2.SelectedItem.Text);
cmd.Parameters.AddWithValue(" ,PatNoVal);
cmd.Parameters.AddWithValue(" ,PatID);
cmd.Parameters.Add(" ,SqlDbType.NVarChar," ].Direction = ParameterDirection.Output;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
// Print(您的ID为:" + cmd.Parameters ["@ ReturnIDcount"].Value.ToString() );
TextBox2.Text = cmd.Parameters [" ].Value.ToString();
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(此 .GetType()," , 警报(" + cmd.Parameters [ @ ReturnIDcount"].Value.ToString()+ ');",捕获(例外)
{
消息(" +例如Message,此);
}
这是我的存储过程..
ALTER 过程 [dbo].[usp_NEW_TRANSACTIONS]
(
@患者 nvarchar ( 50 ),
@ E_TO nvarchar ( 50 ),
@ R_type nvarchar ( 50 ),
@ User_name nvarchar ( 50 ),
@ ReportType nvarchar ( 50 ),
@ Patient_no int ,
@ Patient_ID_NO 数字( 18 , 0 ),
@ ReturnIDcount nvarchar (max)输出
)
AS
开始
声明 @ idcount 数字( 18 , 0 )
声明 @ tempid 数字( 18 , 0 )
set @ tempid = 0 ;
声明 @ idcnt 数字( 18 , 0 )
选择 @ idcnt = isnull(max(idcount), 0 )来自交易其中 year(R_date)= year(getdate())
如果( @ idcnt = 0)
设置 @ tempid = 1
其他
设置 @ tempid = @ idcnt +1
- 结尾处签名:设置@ReturnIDcount ='301A'+ cast(@idcount为nvarchar)
INSERT INTO dbo.Transactions(患者,E_TO,R_date,R_from,用户名,报告类型,患者编号,患者ID_NO, idcount,ReturnIDcount)值( @患者, @ E_TO ,getdate(), @ R_type , @ User_name , @ Patient_no , @ tempid ,选择 @ idcount = isnull(max(idcount), 0 )来自交易其中 year(R_date)= year(getdate())
set @ ReturnIDcount = ' 41/3/' + cast( @ idcount as nvarchar )
返回 @ ReturnIDcount
END
解决方案
SQL return语句需要一个整数值-您正在给它一个nvarchar(max)值,其中包含"41/3/nn",其中nn是idcount.
它尝试将其最难地转换成整数,但是失败了,因为它是一个表达式,一个日期或它通常不理解的东西.结果,它引发异常.
将退货更改为:
返回 @ idcount,一切都很好. /blockquote>
i am getting this error this my c# code
string Patient_name = NameTxtBx.Text, Export_TO = ToTxtBx0.Text; int PatNoVal; PatNoVal = Convert.ToInt32(PatNo.Text); PatNoVal = int.Parse(PatNo.Text); decimal PatID = decimal.Parse(PatID_NO.Text); try { con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "usp_NEW_TRANSACTIONS"; cmd.Parameters.AddWithValue("@Patient", Patient_name); cmd.Parameters.AddWithValue("@E_TO", Export_TO); cmd.Parameters.AddWithValue("@R_type", DropDownList1.SelectedItem.Text); cmd.Parameters.AddWithValue("@User_name", Label1.Text = Session["name"].ToString()); cmd.Parameters.AddWithValue("@ReportType", DropDownList2.SelectedItem.Text); cmd.Parameters.AddWithValue("@Patient_no", PatNoVal); cmd.Parameters.AddWithValue("@Patient_ID_NO", PatID); cmd.Parameters.Add("@ReturnIDcount", SqlDbType.NVarChar, 50); cmd.Parameters["@ReturnIDcount"].Direction = ParameterDirection.Output; cmd.Connection = con; cmd.ExecuteNonQuery(); con.Close(); //Print("Your ID is : " + cmd.Parameters["@ReturnIDcount"].Value.ToString()); TextBox2.Text = cmd.Parameters["@ReturnIDcount"].Value.ToString(); ClientScriptManager cs = Page.ClientScript; cs.RegisterStartupScript(this.GetType(), "IDCount", "alert(" + cmd.Parameters["@ReturnIDcount"].Value.ToString() + "');", true); } catch (Exception ex) { Message("Block 3: Error\nThe Reported fault is:\n" + ex.Message, this); }
this is my stored procedure ..
ALTER PROCEDURE [dbo].[usp_NEW_TRANSACTIONS] ( @Patient nvarchar(50), @E_TO nvarchar(50), @R_type nvarchar(50), @User_name nvarchar(50), @ReportType nvarchar(50), @Patient_no int, @Patient_ID_NO numeric(18,0), @ReturnIDcount nvarchar(max) output ) AS BEGIN declare @idcount numeric(18,0) declare @tempid numeric(18,0) set @tempid = 0; declare @idcnt numeric(18,0) select @idcnt =isnull( max(idcount),0) from Transactions where year(R_date)=year(getdate()) if (@idcnt =0) set @tempid=1 else set @tempid = @idcnt +1 --sign at end : set @ReturnIDcount = '301A' + cast( @idcount as nvarchar) INSERT INTO dbo.Transactions (Patient,E_TO,R_date,R_from,User_name,report_type,Patient_no,Patient_ID_NO,idcount,ReturnIDcount)values (@Patient,@E_TO,getdate(),@R_type,@User_name,@ReportType,@Patient_no,@Patient_ID_NO,@tempid,@ReturnIDcount) select @idcount =isnull( max(idcount),0) from Transactions where year(R_date)=year(getdate()) set @ReturnIDcount = '41/3/' + cast( @idcount as nvarchar) return @ReturnIDcount END解决方案The SQL return statement expects an integer value - you are giving it an nvarchar(max) value which contains "41/3/nn" where nn is the idcount.
It tries it''s hardest to convert this to an integer, but it fails because it is an expression, or a date, or something it generally doesn''t understand. As a result it throws an exception.
Change the return to:
return @idcountand all should be fine.
这篇关于将nvarchar值'41/3/24'转换为数据类型int时,转换失败.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文