• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 848
  • Last Modified:

bind event log source to dropdown list asp.net 2.0 c#

how does one go about binding an event log source to dropdown list asp.net 2.0 c#

<asp:DropDownList id="ddl" runat="server" AutoPostBack="True">
  <asp:ListItem Selected="true">Select a Source</asp:ListItem>

</asp:DropDownList>


protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
   {
    //ddl
    EventLog log = new EventLog(txtLog.Text);
    foreach (EventLogEntry entry in log.Source)
     {
      ddl.Items.Add(log);
      ddl.DataBind();
     }
    }
 }

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
  //fill the panel's label with the ddl selected
  lblResult.Text = txtLogName.Text + "<br/>" + ddl.SelectedItem.Text;

}
0
fwsteal
Asked:
fwsteal
  • 6
  • 6
1 Solution
 
TornadoVCommented:
Try this:

  EventLog evnLog = new EventLog("System");
        foreach (EventLogEntry entry in evnLog.Entries)
        {
            ListItem item = new ListItem(entry.Message,entry.InstanceId.ToString());
            ddl.Items.Add(item);
        }
0
 
fwstealAuthor Commented:
I did this:

EventLog evnLog = new EventLog(LabelLogName.Text);
            foreach (EventLogEntry entry in evnLog.Entries)
            {
                ListItem item = new ListItem(entry.Source.ToString()); //need distinct list
                ddl.Items.Add(item);
            }

how would I get a distinct list?

I also have a <asp:ListItem Selected="True">Select a Source</asp:ListItem> but don't want to be able to select it.
0
 
TornadoVCommented:
EventLog evnLog = new EventLog("System");
        EventLogEntryCollection entriesCollection = evnLog.Entries;

        DataTable table = new DataTable("events");
        DataTable result = new DataTable("distinct");

        DataColumn colItem = new DataColumn("source",typeof(System.String));
        table.Columns.Add(colItem);

        DataRow newRow;
        foreach (EventLogEntry entry in evnLog.Entries)
        {
            newRow = table.NewRow();
            newRow["source"] = entry.Source.ToString();
            table.Rows.Add(newRow);
        }

        table.AcceptChanges();

//        DataTable dt = new DataTable(TableName);
        result.Columns.Add("source" , table.Columns["source"].DataType);

        object LastValue = null;
        foreach (DataRow dr in table.Select("", "source"))
        {
            if (  LastValue == null || !(ColumnEqual(LastValue, dr["source"])) )
            {
                LastValue = dr["source"];
                result.Rows.Add(new object[]{LastValue});
            }
        }

        ddl.DataSource = result;
        ddl.DataTextField = "source";
        ddl.DataBind();

        //ListItem item = new ListItem(entry.Source, entry.InstanceId.ToString());
        //ddl.Items.Add(item);

        ListItem firstItem = new ListItem("Select a Source");
        ddl.Items.Insert(0, firstItem);
        ddl.SelectedIndex = -1;
       
    }

    private bool ColumnEqual(object A, object B)
    {
        if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
            return true;
        if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
            return false;
        return (A.Equals(B));  // value type standard comparison
    }

Enjoy.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fwstealAuthor Commented:
Error      1      Expected class, delegate, enum, interface, or struct

 on private bool function
0
 
TornadoVCommented:
Post your source code
0
 
fwstealAuthor Commented:
sorry my error; noticed that after selecting one of the data values, the negative index is allowed to be selected
0
 
fwstealAuthor Commented:
here is one of the data values:
BApp.Button1_Click

I've got this, but it doesn't work:

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
  lblResult.Text = LabelLogName.Text + "<br />" + ddl.SelectedItem.Text + "<br />"; //BApp.Button1_Click
//display entries per the data value selected  
System.Text.StringBuilder sb = new System.Text.StringBuilder();
EventLog log = new EventLog(LabelLogName.Text);
 foreach (EventLogEntry entry in log.Entries)
 {
  sb.Append("<b>Entry Type:</b> ");
  sb.Append(entry.EntryType.ToString());
  sb.Append("<br /><b>Message:</b> ");
  sb.Append(entry.Message);
  sb.Append("<br /><b>Time Generated:</b> ");
  sb.Append(entry.TimeGenerated);
  sb.Append("<br /><br />");
 }
lblResult.Text = sb.ToString();
}

I can get it to display all the entries, but not the specific entries per the selected item. Any ideas?
0
 
TornadoVCommented:
You could do something like the following, and honestly, I think I've answered your first initial question.

public partial class _Default : System.Web.UI.Page
{
    DataView dataView = new DataView();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EventLog evnLog = new EventLog("System");
            EventLogEntryCollection entriesCollection = evnLog.Entries;

            DataTable table = new DataTable("events");
            DataTable result = new DataTable("distinct");

            DataColumn colItem = new DataColumn("source", typeof(System.String));
            table.Columns.Add(colItem);

            DataRow newRow;
            foreach (EventLogEntry entry in evnLog.Entries)
            {
                newRow = table.NewRow();
                newRow["source"] = entry.Source.ToString();
                table.Rows.Add(newRow);
            }

            table.AcceptChanges();

            //        DataTable dt = new DataTable(TableName);
            result.Columns.Add("source", table.Columns["source"].DataType);

            object LastValue = null;
            foreach (DataRow dr in table.Select("", "source"))
            {
                if (LastValue == null || !(ColumnEqual(LastValue, dr["source"])))
                {
                    LastValue = dr["source"];
                    result.Rows.Add(new object[] { LastValue });
                }
            }

            dataView.Table = table;

            ddl.DataSource = result;
            ddl.DataTextField = "source";
            ddl.DataBind();

            //ListItem item = new ListItem(entry.Source, entry.InstanceId.ToString());
            //ddl.Items.Add(item);

            ListItem firstItem = new ListItem("Select a Source");
            ddl.Items.Insert(0, firstItem);
            ddl.SelectedIndex = -1;
        }
    }

    private bool ColumnEqual(object A, object B)
    {
        if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
            return true;
        if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
            return false;
        return (A.Equals(B));  // value type standard comparison
    }

    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        dataView.RowFilter = "source = '" + ddl.SelectedItem.Text + "'";

       
    }
}
0
 
fwstealAuthor Commented:
I gave the above a try and the select source is still an option to select and populate the result label. Any ideas?
0
 
TornadoVCommented:
Thanks for the grade.

In a nutshell, rather than displaying events on a label, why dont you use a gridview (or datagrid for ASP.NET 1.1)?  Keep all events in a table, then use dataview.RowFilter to select events based on user's selection than display that trimmed down data in your gridview(datagrid)?
0
 
fwstealAuthor Commented:
You've helped me tremendously; completely new to c#

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
 {
  //1. get all log entries where they match the source name of ddl.SelectedItem.Text
  //2. bind data to GridViewShowEntries
  dataView.RowFilter = "source = '" + ddl.SelectedItem.Text + "'";
  GridViewShowEntries.DataSource = dataView.RowFilter;
  GridViewShowEntries.DataBind();
//not sure how?
 }
0
 
TornadoVCommented:
Here you go:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    DataView dataView = new DataView();
    DataTable table = new DataTable("events");
    DataTable result = new DataTable("distinct");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EventLog evnLog = new EventLog("System");
            EventLogEntryCollection entriesCollection = evnLog.Entries;

            DataColumn colType = new DataColumn("type", typeof(System.String));            
            DataColumn colTime = new DataColumn("time", typeof(System.String));
            DataColumn colItem = new DataColumn("source", typeof(System.String));
            DataColumn colCategory = new DataColumn("category", typeof(System.String));
            DataColumn colEvent = new DataColumn("event", typeof(System.String));
            DataColumn colUser = new DataColumn("user", typeof(System.String));
            DataColumn colPC = new DataColumn("computer", typeof(System.String));
            table.Columns.Add(colType);            
            table.Columns.Add(colTime);
            table.Columns.Add(colItem);
            table.Columns.Add(colCategory);
            table.Columns.Add(colEvent);
            table.Columns.Add(colUser);
            table.Columns.Add(colPC);

            DataRow newRow;
            foreach (EventLogEntry entry in evnLog.Entries)
            {
                newRow = table.NewRow();
                newRow["type"] = entry.EntryType.ToString();
                newRow["time"] = entry.TimeGenerated.ToString();
                newRow["source"] = entry.Source.ToString();
                newRow["category"] = entry.Category.ToString();
                newRow["event"] = entry.InstanceId.ToString();
                newRow["user"] = entry.UserName == null ? string.Empty : entry.UserName.ToString();
                newRow["computer"] = entry.MachineName.ToString();                
               
                table.Rows.Add(newRow);
            }

            table.AcceptChanges();

            result.Columns.Add("source", table.Columns["source"].DataType);

            object LastValue = null;
            foreach (DataRow dr in table.Select("", "source"))
            {
                if (LastValue == null || !(ColumnEqual(LastValue, dr["source"])))
                {
                    LastValue = dr["source"];
                    result.Rows.Add(new object[] { LastValue });
                }
            }

            dataView.Table = table;

            ddl.DataSource = result;
            ddl.DataTextField = "source";
            ddl.DataBind();

            ListItem firstItem = new ListItem("Select a Source");
            ddl.Items.Insert(0, firstItem);
            ddl.SelectedIndex = -1;

            ViewState.Add("dataSource", table);
        }
    }

    private bool ColumnEqual(object A, object B)
    {
        if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
            return true;
        if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
            return false;
        return (A.Equals(B));  // value type standard comparison
    }

    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        dataView.Table = ViewState["dataSource"] as DataTable;
        dataView.RowFilter = "source = '" + ddl.SelectedItem.Text + "'";

        GridView1.DataSource = dataView.ToTable();
        GridView1.DataBind();
    }
}
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now