Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7636467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:41:18+00:00 2026-05-31T07:41:18+00:00

So this is the code I have tried, in C#, which failed to give

  • 0

So this is the code I have tried, in C#, which failed to give me the result I needed.

SqlCommand comm = new SqlCommand("exec sys.xp_readerrorlog 0,1,'','',@StartDate,@EndDate,N'Desc'");, conn);
comm.Parameters.AddWithValue("@StartDate", "");
comm.Parameters.AddWithValue("@EndDate", "");

SqlDataReader dr = comm.ExecuteReader();

while (dr.Read())
{
    Console.WriteLine(dr.GetString(0));
}

Basically, I need to extract data from these logs (which gets pulled from the SQL Server through this stored procedure), and it seems that When I use a dataReader, there are no records, and if I use a dataset with data adapter, there are also no tables/records in the dataset. This information is critical for me to query.

Is there a way that I can still query the SQL Server error logs without having to resort to stored procedures?

ANOTHER UPDATE:

The parameters for this extended stored procedures are:

  • Value of error log file you want to read: 0 = current, 1 = Archive, 2 = etc…

  • Log file type: 1 or NULL = error log, 2 = SQL Agent log

  • Search string 1: String one you want to search for

  • Search string 2: String two you want to search for to further refine
    the results

  • Search from start time

  • Search to end time

  • Sort order for results: N’asc’ = ascending, N’desc’ = descending

Another method I tried

SqlCommand comm = new SqlCommand(@"exec sys.xp_readerrorlog 0,1,'','',null,null,N'Desc'", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
Console.WriteLine(ds.Tables.Count); //0 returned: no data in dataset

If i was allowed to use stored procedures to query the data, I could have used this following extract, but it would have been deployed too much and be a pain to maintain and decommission

    IF (EXISTS( SELECT * FROM sys.procedures where name = 'writelogs' ))
BEGIN
    DROP PROCEDURE Writelogs;
END
GO

CREATE PROCEDURE WriteLogs @Servername varchar(40),@InstanceName varchar(40),@Pattern varchar(max),@ParamBeginDate varchar(40), @ParamEndDate varchar(40) AS
BEGIN 

    DECLARE @BeginDate DateTime
    DECLARE @EndDate DateTime
    DECLARE @NextQueryID int
    
    --First we have to convert the timestamps EndDate and BeginDate to something usable
    IF (@ParamBeginDate = 'Beginning')
    BEGIN
        SET @BeginDate = null;  --null will cause sys.xp_readerrorlog to read from beginning
    END
    ELSE IF (@ParamBeginDate = 'Last')
    BEGIN
        SELECT TOP 1 @BeginDate = L.TimeLogged FROM LogTable L ORDER BY L.TimeLogged Desc
    END
    ELSE
    BEGIN
        BEGIN TRY   
            SET @BeginDate = CAST(@ParamBeginDate AS DATETIME);
        END TRY
        BEGIN CATCH
            SET @BeginDate = null;
        END CATCH
    END
    IF (@ParamEndDate = 'Now')
    BEGIN
        SET @EndDate = GETDATE();  --null will cause sys.xp_readerrorlog to read till now
    END
    ELSE
    BEGIN
        BEGIN TRY   
            SET @EndDate = CAST(@ParamEndDate AS DATETIME);
        END TRY
        BEGIN CATCH
            SET @EndDate = GETDATE();
        END CATCH
    END
    
    --Temporary Table to store the logs in the format it is originally written in
    CREATE TABLE TMP
                (LogDate DateTime2
                ,Processinfo varchar(40)
                ,[Text] varchar(max))

    --truncate the milliseconds (else ALL records will be retrieved)
    SET @EndDate= dateadd(millisecond, -datepart(millisecond, @EndDate),@EndDate);
    SET @BeginDate= dateadd(millisecond, -datepart(millisecond, @BeginDate),@BeginDate);
    
    INSERT INTO TMP exec sys.xp_readerrorlog 0,1,'','',@BeginDate,@EndDate,N'DESC';
    SELECT TOP 1 L.TimeLogged FROM LogTable L ORDER BY L.Timelogged desc
    INSERT INTO LogTable
    SELECT @Servername,@InstanceName,T.[text],T.LogDate,GETDATE(),0,0,null,@NextQueryID FROM TMP t WHERE PATINDEX(@Pattern,t.[Text]) > 0;
    DROP TABLE TMP;
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:41:19+00:00Added an answer on May 31, 2026 at 7:41 am

    You can’t use AddWithValue for the dates.

    If the dates are blank, then you need to pass null as the value, not an empty string. Those have completely different meanings.

    To test, open Management Studio and execute the following:

    exec sys.xp_readerrorlog 0,1, '', '', '', ''
    

    That will have zero results. However if you do this:

    exec sys.xp_readerrorlog 0,1, '', '', null, null
    

    You will get back a lot of records.


    BTW, your update is still wrong. The dataset code you have will never do anything. Change it to:

    SqlCommand comm = new SqlCommand(@"exec sys.xp_readerrorlog 0,1,'','',null,null,N'Desc'", conn);
    SqlDataAdapter da = new SqlDataAdapter(comm);
    DataSet ds = new DataSet();
    da.Fill(ds, "sometablename");
    Console.WriteLine(ds.Tables.Count); //0 returned: no data in dataset
    

    Note the fill command…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this C code i have tried to assign pointer addresses of one variable
This code is what i have tried to process the query, either delete or
i have tried this code to redirect a php page.but it s not working
Hello you naughty code animals.. I have tried to get this code to work,
Could anyone please help me convert this code to vb.net, I have tried it
I am trying to enable mod_deflate. I have Apache 2.0+ and tried this code
I have this legacy code which embeds an SWF into an HTML using an
So in all my failed attempts to get jQueryUI working, I have tried this
I have tryed to run this code in my console: script/plugin install git://github.com/apotonick/cells.git ...but
this is function.php of a wordpress theme that NOD32 says this code have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.