Help! i need your kindly help !

G

Guest

i wrote the below code , just want got local machine application Eventlog 's
message , and write to MDB.but it 's can not write the message to Mdb,
somebody can check it for me? thanks!


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data.OleDb;

public class LogTest
{
public static void Main(String[] args)
{

string log="Application";
string machine=".";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);

foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
//Console.WriteLine(strText);

OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= c:\test\TestDb2.mdb" ;



string sqlInsert=@"insert into TestTable(Doc) values (@Doc)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);
cmd1.Parameters.Add("@Doc",System.Data.OleDb.OleDbType.VarChar,100,strText);

conn.Open();
try{
cmd1.ExecuteNonQuery();//insert string
}
finally{
conn.Close();
}
}


}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What error are you getting?

why don't you include a catch block and see what the exception is


cheers,
 
L

Landi

Do you have a folder called test under your c drive? If you don't then you
will get an exception.
Also, like Mr. Machin said, you need to use try catch blocks to see what the
error is. You used try but you didn't catch the exception thrown. Include
the open command in your try block because 95% of the time that's where you
will get errors.

********************************************************
DON'T COPY AND PASTE THIS, NOT TESTED
********************************************************
try
{
conn.open();
cmd1.ExcecuteNonQuery();
}
catch(Exception ex)
{
Console.writeLine(ex.Message.ToString());
}
finally
{
conn.close();
}
One more thing, why are you opening and closing a connection every time you
are inserting an entry. You can open before your foreach loop. Insert inside
your foreach loop and when you get out of your loop close the connection.

Hope this helps,
 
G

Guest

Landi,after i add the catch block, got the "Parameter @doc has no default
value" error.

Landi said:
Do you have a folder called test under your c drive? If you don't then you
will get an exception.
Also, like Mr. Machin said, you need to use try catch blocks to see what the
error is. You used try but you didn't catch the exception thrown. Include
the open command in your try block because 95% of the time that's where you
will get errors.

********************************************************
DON'T COPY AND PASTE THIS, NOT TESTED
********************************************************
try
{
conn.open();
cmd1.ExcecuteNonQuery();
}
catch(Exception ex)
{
Console.writeLine(ex.Message.ToString());
}
finally
{
conn.close();
}
One more thing, why are you opening and closing a connection every time you
are inserting an entry. You can open before your foreach loop. Insert inside
your foreach loop and when you get out of your loop close the connection.

Hope this helps,
--
info@donotspam dowhileloop.com
http://www.dowhileloop.com web development
http://publicjoe.dowhileloop.com -- C# Tutorials

roopeman said:
i wrote the below code , just want got local machine application Eventlog 's
message , and write to MDB.but it 's can not write the message to Mdb,
somebody can check it for me? thanks!


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data.OleDb;

public class LogTest
{
public static void Main(String[] args)
{

string log="Application";
string machine=".";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);

foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
//Console.WriteLine(strText);

OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= c:\test\TestDb2.mdb" ;



string sqlInsert=@"insert into TestTable(Doc) values (@Doc)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);
cmd1.Parameters.Add("@Doc",System.Data.OleDb.OleDbType.VarChar,100,strText);

conn.Open();
try{
cmd1.ExecuteNonQuery();//insert string
}
finally{
conn.Close();
}
}


}
}
 
L

Landi

Why don't you issue simple insert statements?
instead of putting @doc put the value that you want
this stays the same
cmd1.Parameters.Add("@doc",System.Data.OleDb.OleDbType.VarChar,100,strText);
remove this line, this should already be defined in your table schema. It
knows what to expect and it gets anything other then a varchar then it will
blow up. I have only used parameters with stored procedures with MS SQL
Server and not Access so I don't even know how this works in this case.
this stays the same.

then execute against the connection.

--
info@donotspam dowhileloop.com
http://www.dowhileloop.com web development
http://publicjoe.dowhileloop.com -- C# Tutorials

roopeman said:
Landi,after i add the catch block, got the "Parameter @Doc has no default
value" error.

Landi said:
Do you have a folder called test under your c drive? If you don't then you
will get an exception.
Also, like Mr. Machin said, you need to use try catch blocks to see what the
error is. You used try but you didn't catch the exception thrown. Include
the open command in your try block because 95% of the time that's where you
will get errors.

********************************************************
DON'T COPY AND PASTE THIS, NOT TESTED
********************************************************
try
{
conn.open();
cmd1.ExcecuteNonQuery();
}
catch(Exception ex)
{
Console.writeLine(ex.Message.ToString());
}
finally
{
conn.close();
}
One more thing, why are you opening and closing a connection every time you
are inserting an entry. You can open before your foreach loop. Insert inside
your foreach loop and when you get out of your loop close the connection.

Hope this helps,
--
info@donotspam dowhileloop.com
http://www.dowhileloop.com web development
http://publicjoe.dowhileloop.com -- C# Tutorials

roopeman said:
i wrote the below code , just want got local machine application
Eventlog
's
message , and write to MDB.but it 's can not write the message to Mdb,
somebody can check it for me? thanks!


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data.OleDb;

public class LogTest
{
public static void Main(String[] args)
{

string log="Application";
string machine=".";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);

foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
//Console.WriteLine(strText);

OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= c:\test\TestDb2.mdb" ;



string sqlInsert=@"insert into TestTable(Doc) values (@Doc)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);
cmd1.Parameters.Add("@doc",System.Data.OleDb.OleDbType.VarChar,100,strText);
conn.Open();
try{
cmd1.ExecuteNonQuery();//insert string
}
finally{
conn.Close();
}
}


}
}
 
G

Guest

thanks Landi&Ignacio, problem solved !

Landi said:
Why don't you issue simple insert statements?
instead of putting @Doc put the value that you want
this stays the same
cmd1.Parameters.Add("@Doc",System.Data.OleDb.OleDbType.VarChar,100,strText);
remove this line, this should already be defined in your table schema. It
knows what to expect and it gets anything other then a varchar then it will
blow up. I have only used parameters with stored procedures with MS SQL
Server and not Access so I don't even know how this works in this case.
this stays the same.

then execute against the connection.

--
info@donotspam dowhileloop.com
http://www.dowhileloop.com web development
http://publicjoe.dowhileloop.com -- C# Tutorials

roopeman said:
Landi,after i add the catch block, got the "Parameter @Doc has no default
value" error.

Landi said:
Do you have a folder called test under your c drive? If you don't then you
will get an exception.
Also, like Mr. Machin said, you need to use try catch blocks to see what the
error is. You used try but you didn't catch the exception thrown. Include
the open command in your try block because 95% of the time that's where you
will get errors.

********************************************************
DON'T COPY AND PASTE THIS, NOT TESTED
********************************************************
try
{
conn.open();
cmd1.ExcecuteNonQuery();
}
catch(Exception ex)
{
Console.writeLine(ex.Message.ToString());
}
finally
{
conn.close();
}
One more thing, why are you opening and closing a connection every time you
are inserting an entry. You can open before your foreach loop. Insert inside
your foreach loop and when you get out of your loop close the connection.

Hope this helps,
--
info@donotspam dowhileloop.com
http://www.dowhileloop.com web development
http://publicjoe.dowhileloop.com -- C# Tutorials

i wrote the below code , just want got local machine application Eventlog
's
message , and write to MDB.but it 's can not write the message to Mdb,
somebody can check it for me? thanks!


//-----------------------------------------------------------------------
//Wrote by Michael April 30 2005
//-----------------------------------------------------------------------

using System.Text;
using System.Diagnostics;
using System.Threading;
using System;
using System.Text.RegularExpressions;
using System.Data.OleDb;

public class LogTest
{
public static void Main(String[] args)
{

string log="Application";
string machine=".";
EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Console.WriteLine("There are {0} entr[y|ies] in the log:",
aLog.Entries.Count);

foreach (EventLogEntry entry in aLog.Entries)
{

string strText = entry.Message;
//Console.WriteLine(strText);

OleDbConnection conn = new OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= c:\test\TestDb2.mdb" ;



string sqlInsert=@"insert into TestTable(Doc) values (@Doc)";
OleDbCommand cmd1=new OleDbCommand(sqlInsert,conn);

cmd1.Parameters.Add("@Doc",System.Data.OleDb.OleDbType.VarChar,100,strText);

conn.Open();
try{
cmd1.ExecuteNonQuery();//insert string
}
finally{
conn.Close();
}
}


}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top