Tuesday, July 19, 2016

Saturday, March 28, 2015

How to create Excel file from XML using C# code

Hello friends welcome back to my new post today i will show you how to create excel from XML data source using C# code in the window form application.
So lets go the steps are as below:-
Steps:
1)Open Visual studio -- Fille-New-Project-Windows

Select your project name as you want and Click OK Button.


My Project Name Is ExcelToXmlConverter.

2) Drag one button from ToolsBox ...If you are not able so see toolbox from your left hand side pls follow below step else go to 3 step.
Go to View---In the view you can able to see ToolsBox  click it.


3) Set property of button as you want and double click on button .

4) Write a below code in the button click event.

Excel.Application xlApp;
            Excel.Workbook xlWorkbook;
            Excel._Worksheet xlWorksheet;
            object misValue = System.Reflection.Missing.Value;
            DataSet dsexcel = new DataSet();
            XmlReader xmlFile;
            int i = 0;
            int j = 0;

            xlApp = new Excel.Application();
            xlWorkbook = xlApp.Workbooks.Add(misValue);
            xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
            xmlFile = XmlReader.Create("D:\\Product.xml", new XmlReaderSettings());
            dsexcel.ReadXml(xmlFile);
            for (i = 0; i <= dsexcel.Tables[0].Rows.Count - 1; i++)
            {
                for (j = 0; j <= dsexcel.Tables[0].Columns.Count - 1; j++)
                {
                    xlWorksheet.Cells[i + 1, j + 1] = dsexcel.Tables[0].Rows[i].ItemArray[j].ToString();
                }
           
            }
            xlWorkbook.SaveAs("D:\\xml2excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkbook.Close(true,misValue,misValue);
            xlApp.Quit();
            releaseObject(xlApp);
            releaseObject(xlWorkbook);
            releaseObject(xlWorksheet);
            MessageBox.Show("Done.....");




        }
        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            }
            catch (Exception exx)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();

            }
       
        }

code is completed run the program you get good output.

Thank you very much.

If you are not able to run this code pls email to me on my email id that is arjundhilod@gmail.com


Wednesday, May 21, 2014

how to set start page in windows application using c#.net in 2010

Description:

 I didn’t worked on windows application one day we got requirement to work on windows application at that time I don’t’ know anything about windows application even how to set startup page in windows application. I have searched many websites for this after search long time I found answer for this partially.

At that time I decided to write this post because some of the people who don’t know how to set start up page in windows application for those people it gives idea to set start up page.
Open visual studio and create new windows application by default it will create Form1.CS page now if we run application Form1.CS runs by default but if we add another page to our project and we need to set that page as start up page at that time we will get problem for that we need to follow these steps.
To set our page as start up page that page needs to contain following code in the program.cs

***************************************
static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new YourFormName());
            Application.Run(new Login());
            if (Login.IsLogin == 1)
            {
                Application.Run(new MDI_main());
            }
            else
            {
                Application.Exit();
            }
        }
***************************************

Application.Run(Form) methodBegins running a standard application message loop on the current thread, and makes the specified form visible.

YourFormName() add your form name (whatever the page name you have given at the time creation of page) and Add this Main () method in all of your pages after that you will get all the pages list in Startup object list there we have chance to select particular page as start up page. After completion of adding this Main method follow the below steps.



Note :- i am using start page from login so you must put your logic in login.cs page.

Friday, May 16, 2014

For beginners Convert int to string in C#

Dear All,

This is for beginners convert int to string in c#


function test()
{
int int_data=0;
string str_data="";

 int_data=int.parse(textbox.Text);
str_data=int_data.Tostring();

MessageBox.show(str_data);

)

Tuesday, February 18, 2014

ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)

Dear All,

if you face some time error in mysql  sss

ERROR 2003: Can't connect to MySQL server on 'localhost' (10061) 


So Not worry very easy solution 

follow simple step

1)Control Panel\All Control Panel Items\Administrative Tools--Services restart mysql Services than finish.........


Thank you for read.........

 

 

 

Monday, January 27, 2014

Get columns name of a table in SQL SERVER

Dear All

this is for new user those face problem

Get columns of a table SQL SERVER

 In SQL IDE:-

select column_name from information_schema.columns
 where table_name = 'You_TableName'
order by ordinal_position

Note :- same query also work in code-behind.


Thursday, December 19, 2013

Create a Stored Procedure in SQL Server

This topic describes how to create a stored procedure by using SQL Server. 

 Create one table :-

CREATE TABLE  tbl_Students

(
    [Studentid] [int] IDENTITY(1,1) NOT NULL,
    [Firstname] [nvarchar](200) NOT  NULL,
    [Lastname] [nvarchar](200)  NULL,
    [Email] [nvarchar](100)  NULL
)
 
 
Support we insert the following data into the above table:-
 
Insert into tbl_Students (Firstname, lastname, Email)
 Values('Vivek', 'Johari', 'vivek@abc.com')

Insert into tbl_Students (Firstname, lastname, Email)
 Values('Pankaj', 'Kumar', 'pankaj@abc.com')

Insert into tbl_Students (Firstname, lastname, Email)
 Values('Amit', 'Singh', 'amit@abc.com')

Insert into tbl_Students (Firstname, lastname, Email)
 Values('Manish', 'Kumar', 'manish@abc.comm')

Insert into tbl_Students (Firstname, lastname, Email)
 Values('Abhishek', 'Singh', 'abhishek@abc.com')
 
 
Now, while writing a Stored Procedure, the first step will be to write the Create Procedure statement as the first statement, 

Create  PROCEDURE Getstudentname(

@studentid INT          --Input parameter ,  Studentid of the student 

)
AS
BEGIN
SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid 
END


Note: It is not necessary that a stored procedure will have to written. It can be the case when a stored procedure doesn't written any thing. For Example, a stored procedure can be used to Insert, delete or update a sql statement. For Example the below stored procedure is used tp insert value into the table tbl_students.

/*
This Stored procedure is used to Insert value into the table tbl_students. 
*/

Create Procedure InsertStudentrecord
(
 @StudentFirstName Varchar(200),
 @StudentLastName  Varchar(200),
 @StudentEmail     Varchar(50)
) 
As
 Begin
   Insert into tbl_Students (Firstname, lastname, Email)
   Values(@StudentFirstName, @StudentLastName,@StudentEmail)
 End