Skip to main content

Posts

Showing posts from July, 2010

Reading Mixed datatypes from an Excel file with C# or asp.net

In a previous article describing how to read the data from Excel file with help of C#, the end developer may face a problem of not able to read the data from a column that contains mixed data types. For e.g. if you have got a column in which the data looks like this: 125200 568585 F25363 152525 The above data will be read except for a value starting with F . This is because when we read the excel with Oledb driver , there is a " RowsToGuess " property in the registry setting which tells how many rows to scan to govern the datatype of the column. If we set that to 0, all the rows are scanned to determine the datatype. Also we need to use IMEX  attribute setting in the connection string so that the data is always read in the "Text" Format. This will help in removing the problem. The following code shows how to set the IMEX setting: OleDbConnection vConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\rohit.xls;Extended Properties

How to Display Images in the PictureBox used in Windows Apps or Mobile Based Apps.

Normally in windows based application there is a requirement to display image in the picture box which has been read from SQL Server. The image is stored in binary format in SQL Server. So we have to convert raw format read from database into bytes and then use stream to load the bytes in the format of an image. The following code shows how to display the image in the picture box in VB.NET    Dim vbyte() As Byte = CType(myDatarow("Snap"), Byte())    Using ms As New IO.MemoryStream(vbyte)    PictureBox1.Image = New Bitmap(ms)    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage    End Using In the above code, myDatarow is the object of DataRow class that is read and “Snap” is the name of the column in the database that stores the image. 1. So firstly we convert the read image from SQL Server into Bytes array. 2. Then we use MemoryStream to read the bytes into the stream and load the same bytes in the format of image in the PictureBox. 3. We can also the “Si