Monday, November 17, 2014

Linked Server to SQL Server 2000

“SQL Server 2012″ has stopped connecting to “SQL Server 2000″ via linked Servers. As this new version uses a new Native Client version i.e. SQLNCLI11, instead of the old SQLNCLI10. This new client only connects back to 2008R2, 2008 and 2005 only.

So for creating linked server we have to work around.

First create a System DSN for connecting SQL Server 2000 Database in the server where SQL Server 2012 is installed. Now create Linked Server like



EXEC master.dbo.sp_addlinkedserver 
@server = N'NorthWind2000'
@srvproduct=N'MSSQL'
@provider=N'SQLNCLI'
@provstr=N'PROVIDER=SQLOLEDB;
SERVER=NorthWind'
 
EXEC master.dbo.sp_addlinkedsrvlogin 
@rmtsrvname=N'NorthWind2000',
@useself=N'True',
@locallogin=NULL,
@rmtuser=NULL,
@rmtpassword=NULL




Wednesday, October 15, 2014

JQuery based Simple Image Slider

To slide collection of images one by one, we are here using simple JQuery script.

Add following code in the space where you want to show images to slide..

<div>     
    <image ID="img1"  src="ImageGallery/1.jpg" Height="331px" width="100%" />    
</div>
<script type="text/javascript">
        var i = 1;
        function fun() {
            i++;
            document.getElementById("img1").src = "ImageGallery/" + i + ".jpg";
            if (i == 5) //here number of images i want to display in the slide show
            { i = 0; }
        }
        setInterval("fun()", 4000);
</script>  


Description:
Here image gallery has 5 images. Default image is 1.jpg. This image will appear on page load. Image will change after 4 seconds.
 

Saturday, September 6, 2014

Inserting and Retrieving Images From Database

Database: Sql Server 2008 R2
Platform: ASP.NET 4.0 using C#




Inserting into database


Create a table Say EmployeePhoto

Table Structure:

EmployeePhotoId           INT         (PK)

EmployeeNo                  VARCHAR(10)

EmployeePhoto              IMAGE


Step-1: Upload image file to your project folder 


string strFileName = fupEmpPhoto.FileName;



try

{

  if (fupEmpPhoto.HasFile)

  {

     fupEmpPhoto.PostedFile.SaveAs(Server.MapPath("Uploads/" + strFileName));

  }

}

catch (Exception objExp)

{

     lblAlert.Text = objExp.Message;

}



Step-2: Convert Image file in byte array


string strPhotoPath = Server.MapPath("Uploads/" + fupEmpPhoto.FileName);

byte[] EmpPhoto = getPhoto(strPhotoPath);



Step-3: Simply insert into database.



Step-4: Delete uploaded file from project folder.

if (File.Exists(Server.MapPath("Uploads/" + fupEmpPhoto.FileName)))

            File.Delete(Server.MapPath("Uploads/" + fupEmpPhoto.FileName));





Now retrieving image from database

Suppose we need to get image of particular employee, using simple select query we will get the data into data table say dt. Now main challenge is to convert byte array data into image file.

if(dt.Rows.Count>0)

{

            //string strfn = Convert.ToString(DateTime.Now.ToFileTime()) + ".jpg";

            byte[] pic = (byte[])dt.Rows[0][0];



            if (File.Exists(Server.MapPath("Uploads/temp.jpg")))

                File.Delete(Server.MapPath("Uploads/temp.jpg"));



            FileStream fs = new FileStream(Server.MapPath("Uploads/temp.jpg"),           FileMode.CreateNew, FileAccess.Write);

            fs.Write(pic, 0, pic.Length);

           

            fs.Flush();

            fs.Close();

           

            imgEmpPhoto.ImageUrl = "Uploads/temp.jpg";           

}












Friday, August 22, 2014

Auto Hide Div Using jquery

Hiding div is very useful. You can use this functionality for hiding alert message, theme or content. For hiding div I have used javascript.

Method-1:

function autoHideDiv()
{
    $(function ()
    {
        setTimeout(function ()
        {
            $("#divMsg").fadeOut(5000);
        }, 5000);
    });
}


this function start hiding div after 5 seconds and fades out completely in next 5 seconds.

Method-2:

function autoHideDiv()
{   
    $("#divMsg").delay(5000).fadeOut(5000);
}

 

Thursday, July 24, 2014

Quick Fix



1. How to disallow calendar of date picker control to select previous dates?

Set Start Date property of calendar extender to To Date on page load.


2. How to make text box of date picker control readonly? 

Add following code on page load..

txtVisitDate.Attributes.Add("readonly", "readonly");

3. How to show Indian rupees symbol in reports?

Set culture info to "en-IN" by overriding the InitializeCulture() like..

protected override void InitializeCulture()
{

   CultureInfo ci = new CultureInfo("en-IN");
    
   Thread.CurrentThread.CurrentCulture =

   Thread.CurrentThread.CurrentUICulture = ci;

   base.InitializeCulture();
}




and add DataFormatString in bounded field.

<asp:BoundField DataField="SalesAmount" DataFormatString="{0:C}" />


4. How to Update first 100 records of a table of MS SQL Server

UPDATE TOP (100) TABLE1
SET FIELD1 = VALUE1