如何在我的.cs页中保存和制作参数字符串以及如何在表中保存十进制值 [英] How to save and make parameter string in my .cs page and save decimal value in tables

查看:57
本文介绍了如何在我的.cs页中保存和制作参数字符串以及如何在表中保存十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使用户在文本框中输入金额5000,我也想在表格中保存金额(5000.00).为此,我应该使用哪种数据类型(我已经使用了numeric(18,2))并使用了存储过程.但是它只保存int值,因此如何保存类似5000.00的数量以及如何设置参数.我的代码示例如下.

I want to save amount in my table like(5000.00), even if user enter the amount 5000 in the text Box. For that what datatype should i use(i have used numeric(18,2)) and using stored procedure. But it saving only int value, so how to save amount like 5000.00 and how to make parameter. My Example of code is below.

SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.AppSettings.Get("connectionstring");

        SqlCommand cmd = new SqlCommand("Proc_InsertPurchaseDetails", con);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            cmd.Connection.Open();
            cmd.Parameters.Add("@FirmName", SqlDbType.NVarChar, 50).Value = ddlFirmName.SelectedItem.Text;
            cmd.Parameters.Add("@Place", SqlDbType.NVarChar, 50).Value = lblPlace.Text;
            cmd.Parameters.Add("@PurchaseDate", SqlDbType.DateTime).Value = txtPurchaseDate.Text;
            cmd.Parameters.Add("@BillNo", SqlDbType.NVarChar, 50).Value = txtBillNo.Text.Trim();
            cmd.Parameters.Add("@Amount",SqlDbType.Decimal).Value = txtAmount.Text.Trim();
            cmd.Parameters.Add("@Category", SqlDbType.NVarChar, 50).Value = rblCategory.SelectedValue.ToString();
            cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar, 50).Value = lblUserName.Text;
            cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = DateTime.Now.ToString();
            cmd.Parameters.Add("@Msg", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            lblMsg.Text = cmd.Parameters["@Msg"].Value.ToString();
            //cmd.Connection
        }
        catch (Exception Ex)
        {
            lblMsg.Text = Ex.Message;
        }


-------------------------------------------------- --------------------------------


----------------------------------------------------------------------------------

ALTER PROCEDURE [dbo].[Proc_InsertPurchaseDetails]

@FirmName NVarChar(50),
@Place NVarChar(50),
@PurchaseDate DateTime,
@BillNo NVarChar(50),
@Amount Decimal(18,0),
@Category NVarChar(50),
@CreatedBy NVarChar(50),
@CreatedDate DateTime,
@Msg nvarchar(50) Output

AS
BEGIN

	insert into Table_Purchase(FirmName,Place,PurchaseDate,BillNo,Amount,Category,CreatedBy,CreatedDate) 
					values(@FirmName,@Place,@PurchaseDate,@BillNo,@Amount,@Category,@CreatedBy,@CreatedDate)
	set @Msg='Purchase Saved...'
END


谁能帮助我简化问题,在此先感谢


Can any one help me to short out the issues, Thanks in advance

推荐答案

存储过程的输入参数@amount必须从
更改
The input parameter @amount of your stored proc must be changed from
decimal(18,0)




to

decimal(18,2)



(18,2)"中的"2"是小数点分隔符之后的代表位数.说((18,0)""时,您只接受整数.



the "2" in "(18,2)" is the number of representative digits after the decimal separator. When saying "(18,0)" you only accept integers.


回想一下,您为什么还要这么做?您可以不使用".00"保存它,但是当您读取要在GUI上显示的值时,请将区域性设置为本地区域性,并格式化金额"字段以显示货币.例如,它将自动将数字显示为< currency_symbol> 5000.00". £5000.00".

作为一项安全措施,我建议您这样做:

on second thoughts, why do you even want to do it? You can let it get saved without ".00" but when you read the values to show on the GUI, set the culture to the local culture and format the amount field to display currency. It will automatically display figures as "<currency_symbol>5000.00" for e.g. "£5000.00".

Just as a safety measure I would recommend this:

报价:

cmd.Parameters.Add("@ Amount",SqlDbType.Decimal).Value = txtAmount.Text.Trim();

cmd.Parameters.Add("@Amount",SqlDbType.Decimal).Value = txtAmount.Text.Trim();



更改为:



to be changed to this:

decimal val;
decimal.TryParse(txtAmount.Text.Trim(), out val);
cmd.Parameters.Add("@Amount",SqlDbType.Decimal).Value = val;



这样可以确保如果在文本框中输入了垃圾值,那么应该存储"0.00"而不是应用崩溃.



this makes sure that if garbage value is entered in the textbox, then "0.00" should get stored rather than the app crashing.


这篇关于如何在我的.cs页中保存和制作参数字符串以及如何在表中保存十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆