Monthly Archives: September 2011

OverflowException while converting to Byte


 string temp = “1000  255 255 0 243 8 50”;

string[] byteStrings = temp.Split(‘ ‘);

byte[] byteOut = new byte[byteStrings.Length];

for (int i = 0; i < byteStrings.Length; i++)

{

byteOut[i] = Convert.ToByte(byteStrings[i]);

}

While you are converting to byte by using above code, if the value you are converting is greater than 255 or less than 0 it throws an exception “System.OverflowException: Value was either too large or too small for an unsigned byte”

You can over come this problem by using encoding class, it will convert the unicode into a sequence of byte.

 string temp = “1000  255 255 0 243 8 50”;

string[] byteStrings = temp.Split(‘ ‘);

byte[] byteOut = new byte[byteStrings.Length];

for (int i = 0; i < byteStrings.Length; i++)

{

byte[] by1 = StrToByteArray(temp);

}

// C# to convert a string to a byte array.

public static byte[] StringToByteArray(string str)

{

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

return encoding.GetBytes(str);

}

To convert from byte format you can use the below code

public string bytToString(byte[] t)

{

// C# to convert a byte array to a string.

byte[] dBytes = t;

string str;

System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();

str = enc.GetString(dBytes);

return str;

}

Compressed but does not reside in a read-only database or filegroup. The file must be decompressed.


Reason:-

Because of  read/write database’s primary .mdf and .ldf files cannot be created on a compressed drive. When you try to create a database on compressed drive or restore it from the compressed drive you will above error.

Change All Table Datatype in SQL


SQL Trim for all tables in Database


RESTORE DATABASE is terminating abnormally


Problem

While try to restore the database using *.bak file, SQL throws an error message that Restore Database is terminating abnormally.

I got the below error while I try to execute the T-SQL “Restore database <DatabaseName> from disk='<Full Path>\<DatabaseName>.bak’

Msg 3234, Level 16, State 2, Line 1

Logical file ‘XXXXX’ is not part of database ‘XXXXXXX’. Use RESTORE FILELISTONLY to list the logical file names.

Msg 3013, Level 16, State 1, Line 1

RESTORE DATABASE is terminating abnormally.

Again I try to restore with different T-SQL statement

Restore database <DatabaseName> from disk=’<Full Path>\<DatabaseName>..bak’ with

move ‘<Logical PrimaryFileName >’ to ‘<Full Path>\< PrimaryFileName >.mdf’,

move ‘<Logical SecondaryFileName>.’to ‘<Full Path>\< SecondaryFileName>.ndf’,

move ‘<LogicalLogName>’ to ‘<Full Path>\< LogName>.ldf’

Again I Got the same exception as above.

Workaround

While I try to fix this error SQLTEAM forum give me detail for the exception.

The RESTORE FILELISTONLY option allows you to see a list of the files that were backed up.  So for example if you have a full backup you will see all of the data files (mdf) and the log file (ldf).

The RESTORE FILELISTONLY can be use with the backup file (*.bak), that exist on the disk. If you have multiple backup files in a single file then you have to use “WITH FILE = X”

Reason

I executed T-SQL statement RESTORE FILELISTONLY FROM DISK = ‘<Full Path>\<BackupFileName>.Bak’

Output as below:

Then I found out that I wrongly specified <Logical PrimaryFileName >,<Logical SecondaryFileName>,<LogicalLogName>.

<Logical PrimaryFileName >=’LogicalPrimaryFileName’must same as specified in ‘Logical’ column of FILELISTONLY output

<Logical SecondaryFileName>=’Logical SecondaryFileName’ must same as specified in ‘Logical’ column of FILELISTONLY output

<LogicalLogName>=’LogicalLogName must same as specified in ‘Logical’ column of FILELISTONLY output

After I wrote T-SQL Query as below

Restore database <NewDatabaseName> from disk=’<Full Path>\<DatabaseName>..bak’ with

move ‘<LogicaNameOfPrimaryFileGroup in .bak>’ to     ‘<Full Path>\< PrimaryFileName >.mdf’,

move ‘<LogicalNameOf SecondaryFileGroup in .bak >.’to ‘<Full Path>\< SecondaryFileName>.ndf’,

move ‘<LogicalNameOfLog in .bak>’ to ‘<Full Path>\< LogName>.ldf’

Now I can able to successfully restore the database.

%d bloggers like this: