Wednesday, 21 December 2011

Java EE Class [com.microsoft.sqlserver.jdbc.SQLServerDriver] not found.

Error description:
- unable to load SQL server drivers
- need to add it in project

How to add it ?? (Ref to Eclipse 3.7.1 indigo)
- download sqljdbc4.jar & sqljdbc.jar
- Add library into Eclipse project
- Right click on project menu -> properties -> Java build path -> Libraries
-> Add External jar
- Add same sqljdbc4.jar file in Deployment Assembly
Right click on project menu -> properties -> Deployment Assembly -> Add

I hope it can be solved !!

Monday, 31 October 2011

Java: File Read / Write

//******** File reading using scanner
     /**
     * Read given file line by line
     * @param m_strFileName File name
     * @throws Exception File not exist
     * @auther Amit 31/10/2011 
     */
    void printFile(String m_strFileName) throws Exception {

        // Create an instance of File for data file.
        File objFile = new File(m_strFileName);
        if(objFile.exists() == false)
        {
            throw new Exception(m_strFileName + "File does not exist");
        }

        // Reading and displaying file line by line
        Scanner objScanner = new Scanner(objFile);
        while(objScanner.hasNextLine() == true)
        {
            System.out.println(objScanner.nextLine());
        }
        objScanner.close();
    }

//********** Using FileInputStream

  // Get the object of DataInputStream, passing FileInputStream
  DataInputStream in = new DataInputStream(new FileInputStream("anyfile.txt"));
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;

  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   
  {
    // Print the line on the console
    System.out.println (strLine);
  }
  in.close();

Friday, 7 October 2011

c#: XML serialization and deserialization, that is from class to xml and xml to class

Consider following as xml structure



Amit
+91-9033334254
1986-01-01
Male
Single

ASI
IND
2000-01-10




  1. First you need to create class in which you can store xml data- For nested xml tag new class is created
  2. if you don't know how to create class for serialization then just search xsd.exe
    in you c:\ drive, or download from internet.
  3. Follow following command using xsd.exe in cmd
  • C:\xsd.exe Employee.xml
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 2.0.50727.42]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'C:\Employee.xsd'.
  • Above Command give u xml schema, later this schema will create class
  • C:\xsd.exe Employee.xsd /classes
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 2.0.50727.42]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'C:\Employee.cs'.
The Above code will generate required classes automatically,
it may contain three class, Employee, Employee_info, Branch etc
but really we don't need to care about these classes.
let's come to the actual coding


// Creating Object of XML Serializer for Getting Data into Emplyee Object
XmlSerializer objXmlSerializer = new XmlSerializer(typeof(Employees));
TextReader objTextReader;
Stream objStream = null;
FileStream objFileStream = null;

// Generated class from xsd.exe
Employees objEmployees = null;

objFileStream = new FileStream("Employee.xml", FileMode.OpenOrCreate);
objTextReader = new StreamReader(objFileStream);

// Get the xml data into the objEmployees obj
objEmployees = (Employees)objXmlSerializer.Deserialize(objTextReader);
objFileStream.Close();

///
/// Display Employees Records on Console
///

public void DisplayEmployeeInfo(Employees Employee)
{
if (Employee == null )
{
Console.WriteLine(Defination.NoRecordFound);
return;
}

for (int nIndex = 0; nIndex < Employee.Length; nIndex++)
{
// Calculating emp age
TimeSpan objTimeSpan = DateTime.Now - DateTime.Parse(Employee[nIndex].BirthDate);

Console.Write("{0}\t{1}\t{2} {3} ", Employee[nIndex].Name, Employee[nIndex].Telephone, ((int)objTimeSpan.TotalDays / 365), Employee[nIndex].RelationshipStatus);

Console.Write("\t{0}\t{1}\t{2}", Employee[nIndex].Branch[0].Name, Employee[nIndex].Branch[0].CountryCode, Employee[nIndex].Branch[0].EstablishmentDate);
Console.WriteLine();
}
}

// for insert / update just take new emplyees obj and serialize it
Employees objNewEmployees = new Employees();

// fill the required info like u get it in Display() method
...
...
XmlSerializer serializer = new XmlSerializer(typeof(Employees));
TextWriter textWriter = new StreamWriter("New file.xml");
serializer.Serialize(textWriter, objNewEmployees);
textWriter.Close();

Thursday, 6 October 2011

Create Thumbnail for image using c#


// Setting Thumbnail size
int height = 600;
int width = 400;

// Getting desired image
Bitmap objImage = new Bitmap("--file path --");

// Showing image in picturebox, here pictureBox1 is pictureBox control
pictureBox1.Image = objImage.GetThumbnailImage(width, height,null, IntPtr.Zero);

Wednesday, 5 October 2011

How to merge any file with exe in VS2008 C#

If you want to merge any file then you can follow below procedure
  • Open Project Property [Menu/Project/Project Name Properties]
  • Go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
  • Add any file to your project
  • Then from the toolbar above the tab-page, select Add Resource/Add Existing file and select your file or you can also drag and drop file from solution explorer.
You can access that resource file like this
ProjectName.Properties.Resources.Filename

If you want to merge dll file then you can follow below procedure
  • download ILMerge
  • put "ILMerge.exe" in your \WINNT directory
  • In VS.NET, right click project, Properties, Common Properties, Build Events
  • In "Post-build Event Command Line" enter:
    ilmerge /out:$(TargetDir)YOURAPPNAME.exe $(TargetPath) $(TargetDir)YOURDLLNAME.dll
  • Then compile the Release version (not the debug version).
  • In your "bin\Release" directory, you will find a YOURAPPNAME.exe which can be run on its own without the .dll.

Thursday, 25 August 2011

To access a file by two process at a time in C#

FileStream  objReader = File.Open(m_sInputFilename, FileMode.Open,FileAccess.Read,FileShare.Read);

Sunday, 7 August 2011

How to make Asp.net Calendar control Read-Only

C# Code behind

protected void calendarMonth_DayRender(object sender, DayRenderEventArgs e)
{
 // Making calendar day Read-Only, i.e no post back
        e.Cell.Text = e.Day.DayNumberText;
}
- you can disable next and prev month directly from property at design time
- the above code will remove the links of day with simple plain text

Friday, 29 July 2011

To display Month name instead of Month number in SQL Server

-- Consider following statement 
SELECT DATENAME(m, str(Month_Number) + '/1/2011')
-- for eg:
SELECT Year, DATENAME(m, Month + '/1/2011') AS Month,from calendar

-- Here m will be the output value
-- str(Month_Number) function convert month number to string i.e 2 => "2"
-- finally whole string "02/1/2011" is passed as argument

Tuesday, 26 July 2011

To get List of Triggers in SQL Server


SELECT trigger_name = sysobjects.name,
trigger_owner = USER_NAME(sysobjects.uid),
table_schema = s.name,
table_name = OBJECT_NAME(parent_obj),
isupdate = OBJECTPROPERTY( id, 'ExecIsUpdateTrigger'),
isdelete = OBJECTPROPERTY( id, 'ExecIsDeleteTrigger'),
isinsert = OBJECTPROPERTY( id, 'ExecIsInsertTrigger'),
isafter = OBJECTPROPERTY( id, 'ExecIsAfterTrigger'),
isinsteadof = OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger'), [disabled] = OBJECTPROPERTY(id, 'ExecIsTriggerDisabled')
FROM sysobjects INNER JOIN sysusers ON
sysobjects.uid = sysusers.uid
INNER JOIN sys.tables t ON sysobjects.parent_obj = t.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE sysobjects.type = 'TR'

Thursday, 23 June 2011

How to use Check Box in Gridview in Asp.Net

ASP.net with C#
Take a GridView, Connect it to a table.

  •  Go to the 'Edit-Columns' of the GridView. 
  •  Add a 'template-field' 
  •  Hit OK. 
  •  Now, go to the 'Edit-Template' of the GridView. 
  •  In the 'Column[0]' of the Item-Template, drag-drop a CheckBox 
  •  Check out whats the ID of the CheckBox (must be CheckBox1) 
  •  That's it. 

Now Stop 'Editing Template' In the Code-Behind use the following code :

// Add Button code
//GridView1 is Gridview control ..
protected void buttonAdd_Click(object sender, EventArgs e)
{
  // Iterating through Gridview rows
  foreach (GridViewRow gvr in GridView1.Rows)
  {
    // Finding checkbox in a row of data grid control
    CheckBox cb = (CheckBox)gvr.FindControl("CheckOne");
    if (cb.Checked == true)
    {
      // Action to be taken if check box is checked
      Response.Write("<h1>Checked !!</h1>");
    }
  }
}