I've created a search page on a rather large Audit table (populated by triggers on every database). There are 4 fields you can search by (and I put each textbox next to each other): User ID, Table ID, Date, Record ID. This is for programmer's use and high level managers who would know what these fields mean. Record ID is the important field that list the rows of the unique ID that were either UPDATED, INSERTED, DELETED. Like I mentioned, it's of an Audit table we use to monitor who's doing what on our database.
I display the results using ListView
(since I've never used it before and it seems good for this read-only purpose). I've found myself using a ton of else-if's in the search button click. I can't help but think there's a better way. Any suggestions? The last else seems pointless. And the only thing I could put out of scope that would work was ListView1.DataBind();
hence why I repeated a lot of that info in each else if statement.
string ID = txtSearch.Text.ToString().Trim();
string lTABLE = ddlTable.SelectedItem.Text.ToString().Trim();
string lRecord = txtRecordID.Text.ToString().Trim();
string lDATE = misc_class.format_mmddyyyy(txtDate.Text).Trim();
string constr2 = "Server=...000...; Database=Audit;uid=junk;pwd=junk;"; // eliminated real info for privacy
if (ID == "" && lTABLE == "" && lDATE == "" && lRecord == "") //ALL 4 blank
{
string query1 = "SELECT TOP 100 [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (lRecord != "") // if Record ID has any value entered in it, search by it only, disregard other textboxes
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where [tablefk] = '" + lRecord + "' order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (lTABLE == "" && lDATE == "" && lRecord == "" & ID != "") //ID only
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where ([old] = '" + ID + "' or [new] = '" + ID + "') order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (ID == "" && lTABLE == "" && lRecord == "" && lDATE != "") //Date only
{
string query1 = "SELECT TOP 200 [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where CONVERT(date,[entrytime]) = '" + lDATE + "' order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (ID == "" && lDATE == "" & lRecord == "" && lTABLE != "") //Table only
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where [tableName] = '" + lTABLE + "' order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (ID != "" && lTABLE != "" && lDATE == "" && lRecord == "") // User ID & Table
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where [tableName] = '" + lTABLE + "'and ([old] = '" + ID + "' or [new] = '" + ID + "') order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (ID != "" && lDATE != "" && lTABLE == "" && lRecord == "") //User ID & Date
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where CONVERT(date,[entrytime]) = '" + lDATE + "' and ([old] = '" + ID + "' or [new] = '" + ID + "') order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (lTABLE != "" && lDATE != "" && ID == "") //Table & Date
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where CONVERT(date,[entrytime]) = '" + lDATE + "' and [tableName] = '" + lTABLE + "' order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else if (ID != "" && lTABLE != "" && lDATE != "" && lRecord == "") // User ID, TABLE & DATE are filled out
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where CONVERT(date,[entrytime]) = '" + lDATE + "' and [tableName] = '" + lTABLE + "' and ([old] = '" + ID + "' or [new] = '" + ID + "') order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
else // all 4 are filled out (in which case we'd choose the Record ID field to search on)
{
string query1 = "SELECT [pk],[databaseName],[tableName],[tablefk],[fieldname],[old],[new],[userfk],[action],[entrytime],[username] FROM AUDITACT where [tablefk] = '" + lRecord + "' order by entrytime desc";
SqlDataAdapter da = new SqlDataAdapter(query1, constr2);
DataTable table2 = new DataTable();
da.Fill(table2);
ListView1.DataSource = table2;
}
ListView1.DataBind();