Friday, November 20, 2009

Free Date Picker for ASP.NET

It is a very good project to learn C# basics and coding standards. The code is very well organized and properly commented.

Comments, Issues or Bugs in Application:

Please post your valuable comment here so that I will work on your comments to fine tune my next projects.

If you find any difficulty in using or understanding the application, please post your issues here.

If you find any bugs, please report here. I will rectify them. Also, you try to fix any bug you find so that you can understand the application better.

Please feel free to contact me at jagadesh97@gmail.com anytime.


Download Datepicker

Friday, August 14, 2009

XML To TREE VIEW

Use the Following Function to construct Treeview with the help of xml file.

public void XMLToTreeview(TreeView treeView, string fileName)
{

XmlTextReader reader = null;
try
{
// disabling re-drawing of treeview till all nodes are added
treeView.BeginUpdate();

reader =
new XmlTextReader(fileName);

TreeNode parentNode = null;

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{


TreeNode newNode = new TreeNode();
bool isEmptyElement = reader.IsEmptyElement;

newNode.Text = reader.Name;

// add new node to Parent Node or TreeView
if (parentNode != null)
parentNode.Nodes.Add(newNode);
else
treeView.Nodes.Add(newNode);

// making current node 'ParentNode' if its not empty
if (!isEmptyElement)
{
parentNode = newNode;
}
}
// moving up to in TreeView if end tag is encountered
else if (reader.NodeType == XmlNodeType.EndElement)
{


parentNode = parentNode.Parent;

}
else if (reader.NodeType == XmlNodeType.XmlDeclaration)
{ //Ignore Xml Declaration
}
else if (reader.NodeType == XmlNodeType.None)
{
return;
}
else if (reader.NodeType == XmlNodeType.Text)
{
parentNode.Nodes.Add(reader.Value);

}

}
}
finally
{

treeView.EndUpdate();
reader.Close();
}

}

Thursday, August 13, 2009

Tool to Import and Export data from (and to) Excel File

Excel Tool to Import and Export data from (and to) Excel File



This small Application is really help to those who were in the need to get data from excel file and export to excel file

This is the screen shot of the application.





It have 3 buttons

Browse
Generate
Export to Excel

Browse


When user clicks on browse button one popup window will open it gets the filename along with path.



This is the code for browse button click

private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select Excel file only";
ofd.CheckFileExists = false;
ofd.CheckPathExists = true;
ofd.AddExtension = true;
ofd.DefaultExt = "txt";
ofd.ShowReadOnly = true;
ofd.ShowHelp = true;
if (ofd.ShowDialog() == DialogResult.Cancel)
Application.Exit();
strfile = ofd.FileName.ToString();
}
Name and path of the file will be saved in strfile.

Generate Button



Generate Button will connect to the specified file and fill the grid with the excel file content.




coding for Generate Button click



private void button1_Click(object sender, EventArgs e)
{
DataSet objDataset1 = new DataSet();
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strfile + ";Extended Properties=Excel 5.0";
OleDbConnection objConn = new OleDbConnection(ConnectionString);
objConn.Open();
String strConString = "SELECT * from [Sheet1$]";
OleDbCommand objCmdSelect = new OleDbCommand(strConString, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataTable dt = new DataTable();
objAdapter1.Fill(objDataset1, "ExcelData");
dataGridView1.DataSource = objDataset1.Tables[0];
objConn.Close();
}
( Note check with query if your excel file does have sheet1 means just replace sheet1 to your sheet name.)

Export To Excel



Export to Excel button will export the content of grid to excel.



private void button2_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)dataGridView1.DataSource;
exportToExcel(dt, "c:\\Exportedtoexcel.xls");
MessageBox.Show("Exported to Excel successfully");
}


>Download the tool

Tuesday, August 11, 2009

Export to Excel with image.

Export to Excel with Image in VB.NET



This article clearly shows how to export image to excel file in VB.NET. First we look the full code then Description for each and ever statement.

[code]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddImageIntoExcel()
End Sub
Dim Img As Bitmap = Image.FromFile("c:\test1.gif")
'place u r image file name with path in above double quoted area
Private Sub AddImageIntoExcel()
'Create an Excel App
Dim excelApp As New Microsoft.Office.Interop.Excel.Application()
excelApp.Visible = False
'The Excel doc paths
Dim oMissing As Object = System.Reflection.Missing.Value
Dim destFile As String = "C:\test_image.xlsx"
Dim excelFile As String = "C:\Book1.xlsx"
'Open the worksheet file
Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
excelBook = excelApp.Workbooks.Open(excelFile)
Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
excelSheet = CType(excelBook.Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim imgCell As Object = "G1"
Dim range As Microsoft.Office.Interop.Excel.Range
range = excelSheet.Range(imgCell)
range.Select()
Dim newImg As Bitmap = New Bitmap(80, 40)
Me.BackgroundImage = newImg
Using g As Graphics = Graphics.FromImage(newImg)
g.DrawImage(Img, New Rectangle(0, 0, newImg.Width, newImg.Height), _
0, 0, Img.Width, Img.Height, GraphicsUnit.Pixel)
End Using
Clipboard.SetDataObject(newImg)
excelSheet.Paste()
excelSheet.SaveAs(destFile)
'Quit
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
MessageBox.Show("image inserted!")
End Sub

End Class
[/code]

Image


Before going to draw the image in excel file we must load it in our application. we can achieve this by using the following statement.
[code]
Dim Img As Bitmap = Image.FromFile("c:\test1.gif")
[/code]
Or in other words we can say The above statement loads the image in img variable.

create the excel application by using below statement.
[code]
Dim excelApp As New Microsoft.Office.Interop.Excel.Application()
[/code]
create the excel book by the help of below statement
[code]
Dim destFile As String = "C:\test_image.xlsx"
Dim excelFile As String = "C:\Book1.xlsx"
'Open the worksheet file
Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
excelBook = excelApp.Workbooks.Open(excelFile)
[/code]

Set the image displaying area by giving values to imgCell ( In our example its value is G1 so I will starts displaying at G1)

[code]
Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
excelSheet = CType(excelBook.Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim imgCell As Object = "G1"
Dim range As Microsoft.Office.Interop.Excel.Range
range = excelSheet.Range(imgCell)
range.Select()
[/code]

Below is the key statement g.DrawImage function will draw the image in uppropriate place
[code]
Using g As Graphics = Graphics.FromImage(newImg)
g.DrawImage(Img, New Rectangle(0, 0, newImg.Width, newImg.Height), _
0, 0, Img.Width, Img.Height, GraphicsUnit.Pixel)
End Using
[/code]




Then the image will be copied into clipboard and the same will be paste in excel file the file will be saved in destination file path Finally the excel application will be quit. By using following statements.

[code]
Clipboard.SetDataObject(newImg)
excelSheet.Paste()
excelSheet.SaveAs(destFile)
'Quit
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
MessageBox.Show("image inserted!")
[/code]

Friday, April 10, 2009

Create a textbox dynamically and validate for numeric value

Hi

This article deals with how to create the textbox dynamically and validate for numeric value only.

place a empty panel in aspx page(Its name panel1).Add the text box in form load event by using the below code


protected void Page_Load(object sender, EventArgs e)
{
TextBox txt = new TextBox();
txt.Attributes.Add("onkeypress", "return AllowNumeric(window.event);");
Panel1.Controls.Add(txt);
}



txt.Attributes.Add("onkeypress", "return NumericOnly(window.event);");
The above mentioned c# statement links the client onkeypress event with the NumericOnly function

Add the below script function in script area in aspx page







ikeycode value consist the ascii value for the keyed character. if we keyed other than the ascii value of greater than 47 and less than 58 means it w'nt accept. It produces the alert message and won't accept the keyed value.



Friday, April 03, 2009

How to Run the C# file in visual studio command prompt.

It’s a good practice. Starts learning the c# through command prompt. Now we see how to start, execute and run a c# program.

Step 1: open the visual studio command prompt (start => Microsoft visual studio=> visual studio tools =>visual studio command prompt).



Step 2: Type “notepad Filename.cs”.



Step 3: Once u finish the c# program save the file.


Step 4: use csc command to execute
Syntax : csc Filename.cs



Step 5: Enter the exe file name ( once u successfully execute the program. It automatically generate the exe file with the same file name. so type the same file name without extension)


Difference between Java and c#

In java javac filename.java comment create the class file with the class name. Then we can execute the java program by entering java classname.

Where as in c# csc command will create the exe file with the same name. By entering the filename alone we can run the program.

Wednesday, April 01, 2009

10 Tips for Managing Difficult People


Look deeper

1 “People do not come to work to do a bad job or to be difficult,” Matt Brown, a director of YSC, a business psychology consultancy, said. “You need to get to the bottom of what's causing the difficulties. That means finding out what drives and motivates that person and why they might not be feeling great at the moment.”
Marielena Sabatier, the chief executive of Inspiring Potential, a coaching and training company, said: “Time and again I see people blaming the other person and not taking responsibility for the situation. But is that person being difficult in response to your behaviour?”

2 If you go into a conversation thinking that someone is difficult, you will find yourself on the defensive, which is likely to ratchet up the tension between you in a particularly unhelpful manner. “Reframe your thinking,” Miss Sabatier said. “Maybe they are not difficult; maybe they are just different from you.”

Change your actions

3 “When faced with a difficult colleague, we have a better chance of getting them to understand us by focusing on what they need from us,” Gareth English, a senior consultant at OPP, a business psychology consultancy, said. “It can be tempting to think: ‘Why should I change when they are the problem?' But the truth is that they're your problem and if you want it fixed, the most effective way is to take responsibility for the change yourself ... Often, the answer is to change something about yourself first.”

Face up fast

4 Don't let one bad meeting turn into an ongoing difficulty. The longer you ignore a problem, the more entrenched it will get. Often a simple conversation can sort things out immediately. “Encourage people to be honest,” Mr Brown said. “Some people don't want to face up to conflict - they will pretend it's not there or will manage around it - but there is no substitute for being transparent and talking in an adult way.” Don't ignore a difficult relationship because it's with your boss, either. “If you are struggling with someone who is managing you, you really need to get to the bottom of it,” Mr Brown said. At the same time, you need to be respectful of their position and that sometimes they will need things from you that you just have to do.”

Communicate their way

5 Most people respond to a difficult situation by using their usual communication technique, only more so. “Far better to identify how your style ... differs from theirs and adapt your own accordingly,” Mr English said. Martin Wing, the managing partner, Europe, at Kepner-Tregoe, a consultancy, agreed. “Speak to their PA or a colleague to find out how they prefer to receive information, whether as data, words or images.”

Prepare for the worst

6 Telling difficult people bad news will never be pleasant, but negative side-effects can be mitigated with a direct approach. “You need to remove emotion and stay focused on the main points,” Mr Wing said. “Identify how the can be turned around to create the next opportunity.”

Don't reward bad behaviour

7 For example, stop solving other people's problems or they will just keep coming back to you. And don't get drawn into arguments with attention-seekers; even if you win the fight, you've lost the battle.

Be clear and consistent

8 If it is the difficult person's behaviour that's at fault, tell them what needs to change and by when, David Williams, chief executive of Impact International, a leadership development company, said. If the person continues to demonstrate the bad behaviour, tell them straight away - don't ignore it until the next formal meeting.

Focus on goals not methods

9 Difficulties can arise when a conversation starts to be about how to do something, not what has to be done. When this happens, the actual point gets lost under a war for control, Miss Sabatier said. “You need a clear idea of what you want to achieve. Focus on the purpose of the conversation, not on getting your own way.”

Some things can't be fixed

10 “ they are behaving in a difficult way because they are not a good fit with the organisation,” Mr Williams said. “It could be worth putting them on a different type of contract, for example an associate contract, or letting them go entirely.” In other situations, for example when dealing with a bully, disciplinary action may be appropriate.