Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server C#4.0
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()
{  
    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:
 
  Column1, varchar(max),null
  Column2, varchar(max),null
  Column3, varchar(max),null
Posted 26-May-13 22:01pm
Comments
Varun Sareen at 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
Member 9903922 at 27-May-13 3:26am
   
Dear Varun,
I had checked it with debugging but still problem presists..
Varun Sareen at 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 at 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 at 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 at 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 at 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
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 /\jmot 175
4 Shweta N Mishra 161
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 27 May 2013
Copyright © CodeProject, 1999-2014
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