Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
public partial class _Default : System.Web.UI.Page 
{
    SqlConnection cn = new SqlConnection("data source=.; initial catalog= Details; integrated security=sspi");
    SqlDataAdapter adp1, adp2;
    SqlCommandBuilder cmb;
    DataSet ds, ds2;
    byte[] hashValue, MessageBytes;
    string StringtoConvert;
    UnicodeEncoding MyUniCodeEncoding;

    protected void Page_Load(object sender, EventArgs e)
    {
        adp1 = new SqlDataAdapter("select * from client", cn);
        ds = new DataSet();
        adp1.Fill(ds, "emp");
        cmb = new SqlCommandBuilder(adp1);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //TextBox8.Text = "";
        TextBox9.Text = "";
       // StringtoConvert = TextBox2.Text;
        StringtoConvert = TextBox3.Text;
        MyUniCodeEncoding = new UnicodeEncoding();
        MessageBytes = MyUniCodeEncoding.GetBytes(StringtoConvert);
        SHA1Managed SHhash = new SHA1Managed();
        hashValue = SHhash.ComputeHash(MessageBytes);
        foreach (byte b in hashValue)
        {
           // TextBox8.Text = TextBox8.Text + string.Format("{0:X1}", b);
            TextBox9.Text = TextBox9.Text + string.Format("{0:X9}", b);
        }

        adp2 = new SqlDataAdapter("select * from client where UserName='" + TextBox2.Text + "'", cn);
        ds2 = new DataSet();
        adp2.Fill(ds2, "emp");

        if (ds2.Tables["emp"].Rows.Count > 0)
        {
            Response.Write("Username already exists, Try another username");
        }
        else
        {
            DataRow rw = ds.Tables[0].NewRow();
            rw[0] = TextBox1.Text;
            rw[1] = TextBox2.Text;
            rw[2] = TextBox9.Text;
            rw[3] = DropDownList1.Text;
            rw[4] = TextBox6.Text;
            rw[5] = TextBox7.Text;
            ds.Tables[0].Rows.Add(rw);
            //adp1.Update(ds.Tables[0]);
            adp1.Update(ds.Tables[0]);
            Response.Write("Data is added");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

            HyperLink1.Visible = true;
        }
    
    }

I am getting a error in this code adp1.Update(ds.Tables[0]);
error is String or binary data would be truncated.

columns are proper and datatype is proper with appropriate range values
There is no problem in Encryption Code.
Problem occurs while entering data in database.
I am entering only very small values with are within the limit
I am not able to enter data in data base.
I find nothing wrong in the above code but still i get this error.
Any one can explain this

Regards
Amul

Stack Trace:
Line 82:
Line 83:             adp1.Update(ds.Tables[0]);
Line 84:             Response.Write("Data is added");
Line 85:

Source File: e:\Amul Websites\AMUL WEBSITES\ LOGIN PAGE\SignUP.aspx.cs    Line: 83

Stack Trace:


[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]

   System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) +1472823
   System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) +45
   System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) +2257
   System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, D
Posted
Updated 15-Mar-11 5:50am
v4

1 solution

Well, this error message appears when you try to insert a string with more characters than the column can accommodate.

Have a look here with full detail:
Full error details[^]
Similar discussion[^]

So, it looks like you are trying to insert too much of data then it has been set in database.

UPDATE:
I further noticed that you are encrypting it and then storing the password. That must be the reason. Even if your password is 6 letters (and based on that you set range of 10 characters in DB), encryption would be making it some 15-20 letters and thus an error while insert.
Just increase the datatype range and it should go away.
 
Share this answer
 
v3
Comments
Sandeep Mewara 15-Mar-11 12:56pm    
BTW, see my updated answer.

Try to enter the same data directly in SQL without code and see whats happening. Simple update/insert query with encrypted password.
fjdiewornncalwe 15-Mar-11 13:52pm    
+5. Why? Because you are correct.
Amul V V Wayangankar 16-Mar-11 0:11am    
Thanks Sandeep I will increase Datatype Range and check.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900