Wednesday, December 17, 2014

Image From Web URL to Base64 String

Image are Download using Web Client and byte code pass for base64 conversion. 


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Web.UI;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void convert_Click(object sender, EventArgs e)
    {
        byte[] raw;
        raw = new WebClient().DownloadData(txturl.Text);
        Context.Response.Write(CreateBase64Image(raw));
    }

    private string CreateBase64Image(byte[] fileBytes)
    {
        System.Drawing.Image streamImage;
        /* Ensure we've streamed the document out correctly before we commit to the conversion */
        using (MemoryStream ms = new MemoryStream(fileBytes))
        {
            /* Create a new image, saved as a scaled version of the original */
             streamImage = (System.Drawing.Image.FromStream(ms));
        }
        using (MemoryStream ms = new MemoryStream())
        {
            /* Convert this image back to a base64 string */
            streamImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return Convert.ToBase64String(ms.ToArray());
        }
    }

}


Return Like :

Thursday, July 24, 2014

MySQL XML Querying

DROP PROCEDURE IF EXISTS `test223`$$ CALL test223()
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `test223`()
BEGIN
 DECLARE xmlDoc TEXT;
 DECLARE i INT ;
 DECLARE coun INT;
 DECLARE child1 VARCHAR(400);
 DECLARE child2 VARCHAR(400);

 SET i =1;

 SET xmlDoc = '<Data><parent><child1>Example 1</child1><child2>Example 2</child2></parent><parent><child1>Example 3</child1><child2>Example 5</child2></parent><parent><child1>Example 5</child1><child2>Example 6</child2></parent></Data>';

 SET coun = ExtractValue(xmlDoc, 'count(/Data/parent/child1)');

 DROP TEMPORARY TABLE  IF EXISTS `parent`; 
 CREATE TEMPORARY TABLE parent (     child1 VARCHAR(400),   
                                                                              child2 VARCHAR(400) ); 


 WHILE i <= coun DO


     INSERT INTO parent
    SELECT ExtractValue(xmlDoc, '//parent[$i]/child1'), ExtractValue(xmlDoc,   '//parent[$i]/child2');

    SET i = i+1;

 END WHILE;

   SELECT * FROM  parent; 

END$$

DELIMITER ;



Response Table :

+---------------------+
| Child1      | Child2     |
|-----------|-----------|
|Example 1 |Example 2|
|-----------|-----------|
|Example 3 |Example 5|
|-----------|-----------|
|Example 5 |Example 6|
|-----------|-----------|

Sunday, December 1, 2013

How to create, alter and Drop a Constraints in SQL SERVER

To alter a constraints we need to drop that constraints first.
then recreate that constraints.

Follow this syntax, to Drop Constraints.

ALTER TABLE [dbo].[TableName]  DROP CONSTRAINT [FK_Config_ConstraintsName]

How to check a constraint is exist or not ?


REFERENTIAL constraints are saved in REFERENTIAL_CONSTRAINTS table.

SELECT
    *
    FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS

    WHERE CONSTRAINT_NAME   ='FK_EVENT_CONFIGURATION_SCREEN_MASTER'

Tuesday, November 12, 2013

How To Encript and Decript Web.Config CONNECTION STRING

Step to Encript Connection string :

Step 1: Open CMD As Administrator

step 2:  cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

step 3: aspnet_regiis -pc "MyKeys" -exp

 Step 4:
**** For Local Machine ******
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis -pa "MyKeys" "Max70-PC\Max72"

**** For Server Machine ******
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE"

Step 5 : Add This section to your Web.Config File in "configuration" Section

  <configProtectedData>
    <providers>
      <add name="MyProvider"  type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           description="Uses RsaCryptoServiceProvider to encrypt and decrypt"
           keyContainerName="MyKeys"
           useMachineContainer="true"
           />
    </providers>

  </configProtectedData>



**** -each site must have a identity in IIS. take that ID. i.e -Site "4" . ***

Step 6:  C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis -pe "connectionStrings" -site "4"  -app "/" -prov "RsaProtectedConfigurationProvider"



Step to decript Connection string :
Step 1: Open CMD As Administrator

Step 2:  cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

Step 3:   aspnet_regiis -pd "connectionStrings" -site "4"  -app "/"

Ref : http://msdn.microsoft.com/en-us/library/2w117ede.aspx

Tuesday, October 22, 2013

Delete all the foreign key relation in your SQL Server database


DECLARE @cmd nvarchar(1000)
DECLARE @fk_table_name nvarchar(1000)
DECLARE @fk_name nvarchar(1000)

DECLARE cursor_fkeys CURSOR FOR
   SELECT  OBJECT_NAME(fk.parent_object_id) AS fk_table_name,
                  fk.name as fk_name
   FROM    sys.foreign_keys fk  JOIN 
           sys.tables tbl ON tbl.OBJECT_ID = fk.referenced_object_id
   -- WHERE OBJECT_NAME(fk.parent_object_id) in ('table1', 'table2', 'table2')

OPEN cursor_fkeys
FETCH NEXT FROM cursor_fkeys
      INTO @fk_table_name, @fk_name

WHILE @@FETCH_STATUS=0
BEGIN
      -- build alter table statement
      SET @cmd = 'ALTER TABLE [' + @fk_table_name + '] DROP CONSTRAINT [' + @fk_name + ']'
      -- execute it
      exec dbo.sp_executesql @cmd

      FETCH NEXT FROM cursor_fkeys
      INTO @fk_table_name, @fk_name
END
CLOSE cursor_fkeys

DEALLOCATE cursor_fkeys

Saturday, August 24, 2013

How to copy directory using VB.Net



Forder to folder copy :


Dim newWfFilePath As String = Web.HttpContext.Current.Server.MapPath(filepath)
Dim defaultPath As String = Web.HttpContext.Current.Server.MapPath(copyfrom)
Dim directorys() As String = Directory.GetDirectories(defaultPath)
               
For Each folder As String In directorys
          Dim folderName() As String = folder.Split("\"c)
          Dim directoryName As String = folderName(folderName.Length - 1)
 If (Directory.Exists(newWfFilePath + "/" + directoryName)) Then
 Else
     Directory.CreateDirectory(newWfFilePath + "/" + directoryName)
     Dim strFiles() As String = Directory.GetFiles(defaultPath + "/" + directoryName)


     For Each strFile As String In strFiles
          Dim FileName() As String = strFile.Split("\"c)
          System.IO.File.Copy(strFile, newWfFilePath + "/" + directoryName +  
                              "/"+FileName(FileName.Length - 1))

     Next

 End If
Next

SQL Optimization

  SQL Optimization  1. Add where on your query  2. If you remove some data after the data return then remove the remove condition in the sel...