Dot .Net Interview Preparation Starting DotNetFunda 1
Dot Net Funda - 2
Dot Net Funda - 3
Difference between background thread and foreground thread by Shivprasad Koirala?
Image Resizing C# Code According To image Ratio
{
string res = null;
string photoPath = null;
string PhotothumbPath = null;
string newfilename = null;
// int newfileSize = null;
string fileName = Path.GetFileNameWithoutExtension(newfilename);
string fileExt = Path.GetExtension(newfilename);
string thumbName = null;
string fullName = fileName + fileExt;
string uploadDir = Server.MapPath("../uploadImage/");
try
{
if (!Directory.Exists(uploadDir))
Directory.CreateDirectory(uploadDir);
/* for category */
photoPath = uploadDir + "/";
if (!Directory.Exists(photoPath))
Directory.CreateDirectory(photoPath);
PhotothumbPath = photoPath + "Thumb/";
if (!Directory.Exists(PhotothumbPath))
Directory.CreateDirectory(PhotothumbPath);
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
res = "E#There wasn't any file uploaded.";
return res;
}
/* Check file extension (must be JPG) */
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
{
res = "E#The file must have an extension of JPG";
return res;
}
////////************************//*/*/*/
Graphics oGraphics = null;
Bitmap originalBMP = new Bitmap(myFile.InputStream);
Bitmap newBMP = null;
int newWidth = 0, newHeight = 0;
/* Calculate the new image dimensions */
float origWidth = originalBMP.Width;
float origHeight = originalBMP.Height;
if (origWidth > 800)
{
float sngRatio = origWidth / origHeight;
newWidth = int.Parse(ConfigurationManager.AppSettings.Get("ImageWidth").ToString()); //50;
int s = (int)sngRatio;
float newHeight1 = newWidth / sngRatio;
newHeight = (int)newHeight1;
// Create a new bitmap which will hold the previous resized bitmap
newBMP = new Bitmap(originalBMP, newWidth, newHeight); // Create a graphic based on the new bitmap
newBMP.SetResolution(50, 40);
//newBMP.SetResolution(int.Parse(ConfigurationManager.AppSettings.Get("quesImageWidth").ToString()), int.Parse(ConfigurationManager.AppSettings.Get("quesImageHeight").ToString()));
oGraphics = Graphics.FromImage(newBMP);
//newfileSize = newBMP;
}
else
{
newWidth = (int)origWidth;
newHeight = (int)origHeight;
oGraphics = Graphics.FromImage(originalBMP);
originalBMP.SetResolution(50, 40);
//originalBMP.SetResolution(int.Parse(ConfigurationManager.AppSettings.Get("quesImageWidth").ToString()), int.Parse(ConfigurationManager.AppSettings.Get("quesImageHeight").ToString()));
newBMP = originalBMP;
}
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
oGraphics.CompositingQuality = CompositingQuality.HighQuality;
newfilename = myFile.FileName;
fileName = Path.GetFileNameWithoutExtension(newfilename);
fileExt = Path.GetExtension(newfilename);
fileName = fileName; //+ "_" + txt_Name.Text;
fullName = fileName + fileExt;
int i = 0;
while(File.Exists(photoPath + fullName))
{
i++;
fullName = fileName + "_" + i + fileExt;
}
/* Thumb Image */
System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
int thumbHeight = int.Parse(ConfigurationManager.AppSettings["thumbHeight"].ToString());
int thumbWidth = int.Parse(ConfigurationManager.AppSettings["thumbWidth"].ToString());
System.Drawing.Image myThumbnail = newBMP.GetThumbnailImage(thumbWidth, thumbHeight, myCallBack, IntPtr.Zero);
thumbName = fileName + "_Thumb" + fileExt;
myThumbnail.Save(PhotothumbPath + thumbName, ImageFormat.Jpeg);
/* Save Original image */
Bitmap wmBitMap = new Bitmap(newBMP);
wmBitMap.Save(photoPath + fullName, ImageFormat.Jpeg);
UploadFiles.AddFiles(fullName, thumbName, fullName, 0, newWidth, newHeight, "I");
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
}
catch (ArgumentException errArgument)
{
// The file wasn't a valid jpg file
res = "E#The file wasn't a valid jpg file.";
System.IO.File.Delete(photoPath + fullName);
}
res = fullName;
return res;
}
C# Code for Encrypt and Decrypt Password In Binary Format
Encrypt and Decrypt Data In Binary Format
public string EnryptString(string strEncrypted)
{
byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted);
string encryptedPassword = Convert.ToBase64String(b);
return encryptedPassword;
}
public string Decrypter(string encrString)
{
byte[] b = Convert.FromBase64String(encrString);
string decryptedPassword = System.Text.ASCIIEncoding.ASCII.GetString(b);
return decryptedPassword;
Exception handling in sql server 2005 (use of try catch block in stored procedure).
USE [Organization]
GO
/****** Object: StoredProcedure [dbo].[Sp_InsertStudent] Script Date: 05/25/2011 12:15:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[Sp_InsertStudent](@name varchar(50),@marks int)
as
begin
insert into student(#name) values(@name)
end
begin
declare @id int
set @id= (select max(id) from Student)
begin try
insert into marks values(@id,@marks)
end try
begin catch
select 'Error Message:-' Error_Message() as Error_Msg
end catch
end
exec Sp_InsertStudent 'Ashish', 78
Trigger to print the message on value insertion. //give error
create trigger Messageon_Insert on student
after insert
as
if(select count(*) from student where ID= inserted.ID)!=@@rowcount
begin
Print 'Student Detail Inserted successfully'
end
else
Print 'Sorrry, Student Detail not Inserted successfully'
Trigger to delete Student Marks record when Student record is deleted.
Create Trigger DeleteStudentMarks on Student
after Delete
as
begin
delete from marks where id in (Select id from deleted )
end
Insert value in two tables with stored procedure
create proc Sp_InsertStudent(@name varchar(50),@marks int)
as
begin
insert into student(#name) values(@name)
end
begin
declare @id int
set @id= (select max(id) from Student)
insert into marks values(@id,@marks)
end
exec Sp_InsertStudent 'Sumit',87 //it will insert Name in Student table and marks and there corresponding Id in Makrs Table
While Loop for print Table in sql (Example with image)
Sql Self Join example with Images
Create Person table for Self Join example
create table tblPerson(P_ID int primary key, P_Fname varchar(20),P_Lname varchar(20),Gender varchar(10),P_Father int,P_Mother int)
insert into tblPerson values('Puran','Singh','M',null,null)
………….
Self join Example
select a.P_Fname,b.P_Fname from tblPerson as a
join tblperson as b on a.P_ID=b.P_Father
where a.P_Fname='ram'
Left Outer Join with Images
create table Emp(E_Id int, E_Name varchar(30),E_Salary int)
Insert into Emp values(1,'Vikas',14000);
Create Department Table
create table Emp_Department(EmpId int primary key,DName varchar(20));
insert into Emp_Department values(1,'web Development');
Left Outer Join
select a.E_Name as Employee_Name,b.DName as
Department_Name from Emp a left outer join
Emp_department b on a.E_id=b.Empid
Inner Join Example with Images
create table Emp(E_Id int, E_Name varchar(30),E_Salary int)
Insert into Emp values(1,'Vikas',14000);
Create Department Table
create table Emp_Department(EmpId int primary key,DName varchar(20));
insert into Emp_Department values(1,'web Development');
Inner Join Example
select a.E_Name as Employee_Name,b.DName as
Department_Name from Emp a inner join
Emp_department b on a.E_id=b.Empid
Is same as
select a.E_Name as Employee_Name,b.DName as
Department_Name from Emp a join
Emp_department b on a.E_id=b.Empid
Delete Duplicate Record from Table (by using temporary table)
Nth highest Salary Example
select top 1 E_Salary from (select top 3 E_Salary from Emp order by E_Salary desc)as Edfad ORDER BY E_Salary