Link to home
Start Free TrialLog in
Avatar of praneetha
praneetha

asked on

Handling Large data

i have a simple form which inserts data into database. now when user enters large data and the sql datatable datacoulm is defined to take less data it always throws an exception

System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.

so what is the best way to handle the exception. i know i ca set the max length of textbox to match with the datacoumn size..is their any other way to do it..

hmm like catch teh exception and tell the user exactly which field is waytoo bigger than it needs to be..

or how do peple handle such an exception

Thank You
Avatar of Type25
Type25

Personally i just set the maxlength of the textbox to the length of the column, however if it's a multiline textbox i usually setup a custom validator

Avatar of praneetha

ASKER

how do sql helper classes handle such stuff?
How do you mean?

They throw an exception and let you handle it.
all the exception says is that SQL exception is thrown and it doenst really say which column it was...(does it?)

and is there way that i can just chop the data in stored procedure instead of letting it throw the exception...

well i understand it is better to od the max length way..but was just wondering..what happens in a case when i do not know the sql datacolumn size...

(did u mean sql helper classes throw an exception and wants us to handle it...do u know how to find out which column actually failed)
Unfortunately it's a general SqlException, afaik it doesn't tell you the failed column  (99% sure on that)

You can crop the string in your sp no probs, ie;

IF LEN(@yourstring) > columnLength) SET @yourstring = columnLength

If the columnLength is unknown then you'll need to return that from systables (?)

However, in my opinion i wouldn't want to go truncating the text automatically, not at least without telling the user.
yep i understand that's a bad thing to do..just chop with out notification. i guess will go with custom validation for now...

and leave the question open in case anyone has better suggestions

thank you

praneetah,
I agree with Type25, just limit your textbox to column length of database column.
but you can use try and catch statment as following to get around the issue you are having...

in the Catch block put the if statments saying that if the textbox input size is larger then what your database column size is then use the substring function to get however size you want
for instence
try{
  //your code goes here

}
catch(exception err){
   if (textbox.Text.Lenth > "some numbers"){
       textbox.Text.substring("starting point", "Length")
       do rest of the coding...

  }

}//end of try and catch stmt


Hope this helped

Nirav

To be fair you may as well use a custom validator instead of writing the code in your catch block.

That way you can wrap that up in a try catch
Or batter yet just do the substring, so it would chop it off at the very first submission....

e.g. i am assuming you are using insert statment

inser into values(textbox1.text.substring(0,50), textbox2.text.substring(0,25));

that way you dont have to worry about catching it in to try and catch statment and user can put however many length of data yet it would only insert only 50 or 25 or whatever you assign in substring characters into the database...


Nirav
How would you inform the user the data was truncated?  :)
can u give me an eg of how to use costom validator. thank you
Is it possible to change the field type in your table to text or ntext.   You will not face problem of truncation after that.

Best, Nauman.
well i guess i can..but the data is never really that large...and would that also mean changing the stored procedures to take text paramater? and if i understand it right would it not mean...just taking up large amount of memory on sql server bcz of text or ntext...
What is the datatype in your case? char?

-Nauman.
nvarchar
ASKER CERTIFIED SOLUTION
Avatar of Type25
Type25

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HEY  why did u recommend server validate event instead of client validateevent..

i actually do nto see any server validate event but just clientvalidateevent in properties of custom validator
in the validate function can i do this

e.ControlToValidate="txtComment1";
            e.ClientValidationFunction="CustomValidator_ClientValidate";
            

switch the controltovalidate to other textbox when one textbox is validated...instead of dragging and dropping as many custom validators as textboxes...
That's the thing you don't need to, as long as it has a controltovalidate set you can do what you like in one event.

I didn't necessarily recommend the server validate event, you can do it with JS if you wish but i usually can't be bothered and just do it server side :)


oh..and there is definiately a serverValidate event for the custom validator
Just make sure you're looking at the events view in the properties window
Anyhow, it's pub time for me!

Catch up tomorrow with this one.

Have a good one!

yep got it. i was not looking at eevnts.

so anyways r u saying
>> That's the thing you don't need to, as long as it has a controltovalidate set you can do what you like in one event.

so say i need to validate textbox1,2,3,4

and i set control to validate to textbox1

but in the server validate event..

and i check the 2,3,4 length and display the error message...hmm but the errormessage will be displayed on the custom validator..like say i have it next to textbox1 ...even though textbox 2 fails..it would still display error next to textbox1 right?



>> I didn't necessarily recommend the server validate event, you can do it with JS if you wish but i usually can't be bothered and just do it server side :)

I can debug easier as well!  Which is nice when you're in a hurry most of the time.....as i often am as a contractor...

Right...really am going. See yas.
>> and i check the 2,3,4 length and display the error message...hmm but the errormessage will be displayed on the custom validator..like say i have it next to textbox1 ...even though textbox 2 fails..it would still display error next to textbox1 right?


Yes, but you can stick just standard asp.net labels next to the text boxes and set those:

if (textBox1.length > 50)
    label1.Text = "Error";

if (textBox2.length > 20)
   label2.Text = "Error";  

etc etc...
Thank You