Friday, May 27, 2011

How to check from your Vb6 code if your Access database returns an empty string.

Sometimes if you are connecting to an Access DB , you may need to find if some field value is empty and perform some function on the basis of it .You may use the IsNull function to find if the field is empty in the db.

Dim cn As New ADODB.Connection
 cn.CursorLocation = adUseClient
 cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\stduser\Desktop\db1.mdb;Persist Security Info=False"

 Dim sql As New ADODB.Command
 sql.ActiveConnection = cn
 sql.CommandText = "Select City from Table1 where Country=" & "'" & Me.Field1.Text & "'"

 Dim rec As New ADODB.Recordset
 rec.Open sql

 If IsNull(rec.Fields(0).Value) Then
    -------  
Else
   ------------  
 End If

Tuesday, May 3, 2011

Automatic Properties C#

Just use code as following for automatic properties in C#. You do not need to write the  tedious way as earlier.

 public Bitmap GetWaterMark
        {
            get;
            set;
        }

Monday, May 2, 2011

How to make an image transparent.

Sometimes, we are needed to make an image transparent-.to use it as an watermark or something simillar. We need to use code as following to convert an existing image to an transparent one.

public Bitmap createwatermark(string filepath)
        {
            float[][] matrixItems ={
   new float[] {1, 0, 0, 0, 0},
   new float[] {0, 1, 0, 0, 0},
   new float[] {0, 0, 1, 0, 0},
   new float[] {0, 0, 0, 0.3f, 0}, // the fourth value (0.3f) is the alpha value for partial transparency....
   new float[] {0, 0, 0, 0, 1}};
            ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
            ImageAttributes imageAtt = new ImageAttributes();
            imageAtt.SetColorMatrix(
               colorMatrix,
               ColorMatrixFlag.Default,
               ColorAdjustType.Bitmap);
            Bitmap sourceimage = new Bitmap(filepath);
            Bitmap bitmap = new Bitmap(sourceimage.Width, sourceimage.Height);
            Graphics g = Graphics.FromImage(bitmap);

            g.DrawImage(sourceimage, new Rectangle(0, 0, bitmap.Width, bitmap.Height),0.0f,0.0f,sourceimage.Height,sourceimage.Height,GraphicsUnit.Pixel,imageAtt);
            return bitmap;
        }


Thursday, January 13, 2011

Error The underlying connection was closed: The connection was closed unexpectedly in WCF

I found something interesting today.You will observe that if you  have a WCF service returning  a DataTable and if you try using that DataTable in an APP, then it will error out as follows

The underlying connection was closed: The connection was closed unexpectedly

This will occur if you use code as follows in the DataTable

public DataTable GetDetails(String _course)
    {
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("TopicID", typeof(String)));
        dt.Columns.Add(new DataColumn("TopicName", typeof(String)));

       
        XmlDocument _doc = new XmlDocument();
        _doc.Load(HostingEnvironment.ApplicationPhysicalPath + @"App_Data/Courses.xml");

        XmlNodeList _nodelst = _doc.SelectNodes("//Course[@name='" +_course.ToString() + "']/Topic");

        foreach (XmlNode _node in _nodelst)
        {
        
            dt.Rows.Add(_node.Attributes["id"].InnerText,_node.Attributes["title"].InnerText);
        }

        return dt;
      
    }


The solution to this problem is very simple as I found out.All you would need to do is give a name to the DataTable i.e just replace the code as         DataTable dt = new DataTable(); to         DataTable dt = new DataTable("Test");