Click here to Skip to main content
12,996,709 members (77,648 online)
Rate this:
Please Sign up or sign in to vote.
See more:
In my project I am generating a random character using the Random Number generator function and Storing it in the database, the number generates correctly but during insert of the data it is throwing the Exception “String or binary data would be truncated”

My code:

protected void trigger()
        DataSet ds = ExamManagement.SP.table2_SP_Selectall().GetDataSet();
        if (ds.Tables[0].Rows.Count > 0)
            string a = RandomNumberGenerator(4);
            string b = RandomNumberGenerator(4);
            string c = RandomNumberGenerator(4);
    catch (SqlException ex)
        ClientMessaging("Error :"+ex);
public static string RandomNumberGenerator(int length)
    System.Security.Cryptography.RandomNumberGenerator rng = System.Security.Cryptography.RandomNumberGenerator.Create();
    char[] chars = new char[length];
    //based on your requirment you can take only alphabets or number 
    string validChars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXzZ";
    byte[] bytes = new byte[length-1];
    for (int i = 0; i < length; i++)
        Random rnd = new Random(bytes[0]);
        chars[i] = validChars[rnd.Next(validChars.Length)];
    return (new string(chars));

My SQL Server Table Structure:

Column1, varchar(max),null
Column2, varchar(max),null
Column3, varchar(max),null
Posted 26-May-13 21:01pm
Varun Sareen 27-May-13 3:15am
Dear Friend,

Your code seems fine to me. Have you checked the return value for your code through debugging? If not then kindly check it. If still the problem persists. Then reply to this.


Member 9903922 27-May-13 3:26am
Dear Varun,
I had checked it with debugging but still problem presists..
Varun Sareen 27-May-13 4:31am
What is the length of return value of the string (code) which you are getting from the RandomNumberGenerator() method?
Member 9903922 28-May-13 0:04am
@Varun there is no error in the RandomNumberGenerator() and the Insert Table the error is with the column in a table insertion that is initiated via trigger
David_Wimbley 27-May-13 14:13pm
This code has nothing to do with the cause of your Sql Error. The reason i say this is that varchar(max) is capable of storing up to 2,147,483,647 characters of information in a single column (i believe that is correct)...i highly doubt your code is generating a string that long.

What you need to post is your INSERT statement/code that is creating your insert statement.

I would also suggest that you learn the basics of debugging as well.Use break points/debugger in visual studio, get all values that are being inserted and create an insert statement.

Ex: INSERT INTO TableName (Column1, Column2, Column3) VALUES ('BIG STRING 1', 'BIG STRING 2', 'BIG STRING 3').

Simply put...this is where your issue lies (as your exception stated).

Once you are able to figure out what column is failing you can then begin to adjust what your c# code is doing/causing this issue.
Member 9903922 28-May-13 0:02am
@ David_Wimbley the Size of the parameter in the table is Correct, the issue is that the values will be combined and Inserted into another Table that insertion is initiated by trigger that column length is small thats why the Exception has Occured
Varun Sareen 28-May-13 0:08am
exactly the same i was trying to say 5!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170609.2 | Last Updated 27 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100