Click here to Skip to main content
15,886,812 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace ExportDataTableApplication
{
    class ExportDataTable
    {
        DateTime datetime = DateTime.Now;
        //Execute stored procedure
        static void Main(string[] args)
        {
            Console.WriteLine("Running App");

            DataSet ds = new DataSet();
            DateTime datetime = DateTime.Now;

            using (SqlConnection conn = new SqlConnection("Test connection string"))
            {
                string filePath = @"C:\Error.txt";
                try
                {
                    SqlCommand sqlComm = new SqlCommand("Test", conn);

                    sqlComm.CommandType = CommandType.StoredProcedure;

                    SqlDataAdapter da = new SqlDataAdapter(sqlComm);

                    da.Fill(ds);
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    using (StreamWriter writer = new StreamWriter(filePath, true))
                    {
                        writer.WriteLine("Date :" + DateTime.Now.ToString() + "=> Exception :" + e.Message + "<br/>" + Environment.NewLine + "StackTrace :" + e.StackTrace +
                           "" + Environment.NewLine);
                        writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                    }
                }
Posted
Updated 3-Aug-15 5:29am
v5
Comments
[no name] 3-Aug-15 10:32am    
"Is it possible to call a function with parameter with this code?", yes it is possible.
"Putting the parameter as " " so that it reads the message string which it retrieve?", makes no sense.
Member 11878313 3-Aug-15 10:33am    
Do you have any suggestions?
[no name] 3-Aug-15 10:40am    
Suggestions for what? For something that doesn't make any sense to begin with?
Sergey Alexandrovich Kryukov 3-Aug-15 10:47am    
Suggestions for what?! You did not explain what you are trying to achieve.
—SA
OriginalGriff 3-Aug-15 10:47am    
What Wes means is that this is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

With your home-baked logging approach, you will face many problems. Don't do it.

I already answered you other question, so please use my advice: May I know with this code where will the files be stored to? and is the logging code workable?[^].

—SA
 
Share this answer
 
It is possible.
You have to create object and call that method on that object like this:

LogObject o = new LogObject();
o.log("This is my message");

or

var myMessage = "This is my message";
o.log(myMessage);

Second option is make the method static

And than you can call it like this:
LogObject.log("This is my message");

Is this what you wanted to know? or are you looking for something completely different?

Updated after comment:

If I get it right you are trying to write exception into a log file.
try this:

try
{
//your code
}
catch(Exception e)
{
LogObject.log(e.Message);
}

Is this what you need?
 
Share this answer
 
v2
Comments
Member 11878313 3-Aug-15 10:50am    
What I want to put in String message is actually all the exceptions logs of the program instead of a static message. thanks I hope you can understand what i am trying to get here
xszaboj 3-Aug-15 11:18am    
check out updated solution ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900