“字符串或数据将被截断” [英] "String or data would be truncated"

查看:68
本文介绍了“字符串或数据将被截断”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在创建一个软件来帮助我健身房,我想向客户添加一个计划,但是当我添加它时,它会给我这个错误字符串或数据会被截断。< br $>




这是我实际拥有的保存按钮的代码。



Hello guys, I'm creating a software to help me out on my gym and I want to add a plan to a client, but when I add it it gives me this error "string or data would be truncated".


This is the code i actually have for the save button.

try
{
    if (txtMemberName.Text == "")
    {
        MessageBox.Show("Introduza os dados de cliente", "",
        MessageBoxButtons.OK, MessageBoxIcon.Warning);
        txtMemberName.Focus();
        return;
    }
    if (txtMonths.Text == "")
    {
        MessageBox.Show("Introduza o tempo de subscrição (meses)", "",
        MessageBoxButtons.OK, MessageBoxIcon.Warning);
        txtMonths.Focus();
        return;
    }
    if (cmbMembershipType.Text == "")
    {
        MessageBox.Show("Escolha o tipo de plano", "",
        MessageBoxButtons.OK, MessageBoxIcon.Warning);
        cmbMembershipType.Focus();
        return;
    }
    if (txtDiscountPer.Text == "")
    {
        MessageBox.Show("Introduza o desconto", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        txtDiscountPer.Focus();
        return;
    }
    if (txtTotalPaid.Text == "")
    {
        MessageBox.Show("Introduza o valor pago", "",
        MessageBoxButtons.OK, MessageBoxIcon.Warning);
        txtTotalPaid.Focus();
        return;
    }
    double val1 = 0;
    double val2 = 0;
    double.TryParse(txtGrandTotal.Text, out val1);
    double.TryParse(txtTotalPaid.Text, out val2);
    if (val2 > val1)
    {
        MessageBox.Show("O total não pode ser maior do que o valor pago", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtTotalPaid.Text = "";
        txtTotalPaid.Focus();
        return;

    }
    cc.con = new SqlConnection(cs.DBConn);
    cc.con.Open();
    string ct = "SELECT * FROM CustomerMembership WHERE DateFrom <= '" + dtpDateTo.Value.Date + "' AND DateTo >= '" + dtpDateFrom.Value.Date + "' and CustomerID=" + txtM_ID.Text + "";
    cc.cmd = new SqlCommand(ct);
    cc.cmd.Connection = cc.con;
    cc.rdr = cc.cmd.ExecuteReader();
    if (cc.rdr.Read())
    {
        MessageBox.Show("A mensalidade ainda não expirou.." + "\n" + "O pagamento não é permitido", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        if ((cc.rdr != null))
        {
            cc.rdr.Close();
        }
        return;
    }
    cc.con = new SqlConnection(cs.DBConn);
    cc.con.Open();
    string cb = "insert into CustomerMembership(CM_ID,BillDate,CustomerID,MembershipID,DateFrom,Months,DateTo,ChargesPerMonth,TotalCharges,DiscountPer,DiscountAmount,SubTotal,VATPer,VATAmount,ServiceTaxPer,ServiceTaxAmount,GrandTotal,TotalPaid) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17,@d18)";
    cc.cmd = new SqlCommand(cb);
    cc.cmd.Connection = cc.con;
    cc.cmd.Parameters.AddWithValue("@d1",txtMembershipID.Text);
    cc.cmd.Parameters.AddWithValue("@d2",dtpBillDate.Value);
    cc.cmd.Parameters.AddWithValue("@d3",txtM_ID.Text);
    cc.cmd.Parameters.AddWithValue("@d4", txtMembershipTypeID.Text);
    cc.cmd.Parameters.AddWithValue("@d5", dtpDateFrom.Value);
    cc.cmd.Parameters.AddWithValue("@d6", txtMonths.Text);
    cc.cmd.Parameters.AddWithValue("@d7", dtpDateTo.Value);
    cc.cmd.Parameters.AddWithValue("@d8", txtChargesPerMonth.Text);
    cc.cmd.Parameters.AddWithValue("@d9", txtTotalCharges.Text);
    cc.cmd.Parameters.AddWithValue("@d10",txtDiscountPer.Text);
    cc.cmd.Parameters.AddWithValue("@d11",txtDiscountAmount.Text);
    cc.cmd.Parameters.AddWithValue("@d12",txtSubTotal.Text);
    cc.cmd.Parameters.AddWithValue("@d13", txtVATPer.Text);
    cc.cmd.Parameters.AddWithValue("@d14", txtVATAmount.Text);
    cc.cmd.Parameters.AddWithValue("@d15", txtServiceTaxPer.Text);
    cc.cmd.Parameters.AddWithValue("@d16", txtServiceTaxAmount.Text);
    cc.cmd.Parameters.AddWithValue("@d17", txtGrandTotal.Text);
    cc.cmd.Parameters.AddWithValue("@d18", txtTotalPaid.Text);
    cc.cmd.ExecuteReader();
    cc.con.Close();
    st1 = lblUser.Text;
    st2 = "Foi adicionada uma nova mensalidade com o id '" + txtMembershipID.Text + "' ao membro '" + txtMemberName.Text + "'";
    cf.LogFunc(st1, System.DateTime.Now, st2);
    btnSave.Enabled = false;
    MessageBox.Show("Gravado com sucesso", "Registo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}





Sql table:



Sql table:

CREATE TABLE [dbo].[CustomerMembership] (
    [CM_ID]            INT            NOT NULL,
    [CustomerID]       INT             NOT NULL,
    [MembershipID]     INT             NOT NULL,
    [DateFrom]         DATETIME        NOT NULL,
    [Months]           VARCHAR (2)     NOT NULL,
    [DateTo]           DATETIME        NOT NULL,
    [ChargesPerMonth]  DECIMAL (18, 2) NOT NULL,
    [DiscountPer]      DECIMAL (3)     NOT NULL,
    [VATPer]           DECIMAL (3)     NOT NULL,
    [VATAmount]        DECIMAL (3)     NOT NULL,
    [ServiceTaxPer]    DECIMAL (3)     NOT NULL,
    [ServiceTaxAmount] DECIMAL (3)     NOT NULL,
    [TotalCharges]     DECIMAL (18, 2) NOT NULL,
    [BillDate]         DATETIME        NOT NULL,
    [SubTotal]         DECIMAL (18, 2) NOT NULL,
    [GrandTotal]       DECIMAL (18, 2) NOT NULL,
    [TotalPaid]        DECIMAL (18, 2) NOT NULL,
    [DiscountAmount]   DECIMAL (3)     NOT NULL,
    CONSTRAINT [PK_CustomerMembership] PRIMARY KEY CLUSTERED ([CM_ID] ASC),
    CONSTRAINT [FK_CustomerMembership_Customer] FOREIGN KEY ([CustomerID]) REFERENCES [dbo].[Customer] ([C_ID]) ON UPDATE CASCADE,
    CONSTRAINT [FK_CustomerMembership_Membership] FOREIGN KEY ([MembershipID]) REFERENCES [dbo].[Membership] ([M_ID]) ON UPDATE CASCADE
);





我尝试过:



尝试将CM_ID设置为VARCHAR,然后编程一种算法将id设置为I- {number}而不是{number}。

尝试根据Add设置值功能

Ex:

...添加(@ d1,SqlDbType.Int).Value = txtMemberShipID.Text;

但是这个也没用。



任何想法?



What I have tried:

Tryied to set the CM_ID to VARCHAR and then program a sort of algorithm to set the id to I-{number} instead of just {number}.
Tryied to set the Values according to the Add function
Ex:
...Add("@d1", SqlDbType.Int).Value = txtMemberShipID.Text;
But this didn't work too.

Any ideias?

解决方案

错误消息非常明确:

The error message is pretty explicit:
String or data would be truncated

它是什么表示您传递的字段定义太长了。使用调试器确切地查看要传递给SQL的值(特别注意Months字段),并将应用程序更改为不发送太多数据,或者将表更改为接受要发送的数据。

What it means is you are passing a value that is too long for the field definition. Use the debugger to look at exactly what values you are passing to SQL (pay particular attention to the Months field) and either change your app to not send so much data, or your table to accept the data you are sending.


要为每次插入生成
I-{number}

形式的唯一CM_ID,首先将CM_ID的字段类型更改为varchar,然后< a href =https://www.codeproject.com/Tips/1024156/Ask-Your-Database-for-that-Unique-ID>向您的数据库询问该唯一ID [ ^ ]

for each insertion, first change the field type of CM_ID to varchar, then Ask Your Database for that Unique ID[^]


由于表列大小与您为推送传递的数据不匹配而发生此问题。在调试模式下,检查传递的数据长度是否大于该列的大小。
This issue occurred due mismatch between table column size and the data which you passed for pushing. In debug mode check the data length which you pass is more than the size of that columns.


这篇关于“字符串或数据将被截断”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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