Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
protected void trigger()
{  
    try
    {
        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);
            ExamManagement.SP.table1_insert(a,b,c).Execute();
        }
    }
    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++)
    {

        rng.GetBytes(bytes);

        Random rnd = new Random(bytes[0]);

        chars[i] = validChars[rnd.Next(validChars.Length)];

    }

    return (new string(chars));
}

My SQL Server Table Structure:

SQL
Column1, varchar(max),null
Column2, varchar(max),null
Column3, varchar(max),null
Posted
Comments
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.

Regards

Varun
Rajesh Kumar Sowntararajan 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?
Rajesh Kumar Sowntararajan 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.

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