3

byte number = 1; add(number); // form .cs

public static int Add( byte? order)
{
  arParams[0] = new SqlParameter("@number", (number.HasValue) ? ((object)number) : DBNull.Value);
  // stored procedure call is made which takes paramaters, 
}

Stored procedure looks like this

@number tinyint
AS
BEGIN
IF @number IS NOT NULL
BEGIN
  UPDATE 
    table1
  SET 
    number = number + 1
  WHERE 
    id=13
END
INSERT INTO 
 table1
(
  number
)
VALUES 
( 
  number=@number
)

///////why i am getting this error can any one illustrate please and how do i solve this

2 Answers 2

5

The range of tinyint is 0-255.

You're attempting to put 256 into a datatype that doesn't know what 256 is.

int, bigint, smalltint and tinyint ranges.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the quick reply , since i dint understood how its happening. what will be the value of @number in the storedprocedure at first and what will be the value after set number = number+1 ?
You can apply validation before sending it to database. for between 0 and 255 both are inclusive
where can i do that in codebehind?, i am still looking for the answer of this question, i have number = 1 and which is passed to storedprocedure as (object)number, in storedprocedure will the value of @number be 255?
From the code you've posted, your update query is not using the @number parameter at all. Have you checked the 'number' field in table1?
ya i got that, will it fix my problem if i user @number in my update.
|
3

It is because you cannot set the Tinyint value beyond 255 and below 0. So you should apply validation before sending it to database.

8 Comments

thanks , where am i passing 255 and its being set to 256. i am not sure what does (object)number passes to storedprocedure ? and how @number is 255
It's because you are incrementing in your stored procedure. number = number + 1. Keep it 255 if the value is 255 already or you can change the datatytpe to int
ok, so it means after (object)number i am trying to assign it to tinyint the value of @number will be 255. 1 byte = 255 int is it?
You should change the datatype here as well.
now what does this mean, (thanks for your patience and support)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.