Using MS Visual Studio 2012 Express, Using C#.NET 4.0.
Hi guys, This I believe is a simple one.
I’ve been rushing a program at the request of my boss, But now it turns out that I have a lot more free time on it.
So I’m going through the code trying to make it more compact, cleaner etc.
I have this one function…..
public void RunMonth()
{
//** top 10 listings*********************//
SetTxtBox1(DateTime.Now.ToString() + " ==== END OF MONTH REPORTS starting.........\r\n");
SetTxtBox1(" **************************************************************************************\r\n");
try
{
VRMtableDESC = querys.TopVRM("DESC");
SetTxtBox1(DateTime.Now.ToString() + " ==== VRM Top has been loaded\r\n");
}
catch
{
SetTxtBox1(DateTime.Now.ToString() + " ==== VRM Top has FAILED\r\n");
}
}
“VRMtableDESC” is a datatable, one of around 18 datatables. for each datatable this try statement is run.
Now as you can tell this currently is repeated 18 times for every table.
instead i would like to loop into the tables instead and fill them with there data.
is there a way to do this? i would need some sort of collection for the datatables and the query.function calls too as far as I’m aware.
So far i have hit a blank so im hoping there is something im missing.
Thanks in Advance
::::UPDATE::::
Ok so I have this set up so far….
foreach (DataTable tbl in MyDataSet.Tables)
{
tbl.TableName.Equals(querys.TopVRM(""));
}
the equals method needs to be dynamic in its method selection too…. any ideas?
::UPDATE(31/10/12)::
Hi again, Im getting an error, the common error ” object not sent to instance of object”
it apears on the merge in the foreach loop.
I have debugged my query call and looks fine.
var tables = new SortedDictionary<string, Func<DataTable>>()
{
{"VRMtableDESC", () => querys.TopVRM("DESC")},
{"VRMtableASC", () => querys.BotVRM("ASC")},
{"SpectableDESC", () => querys.TopSpec("DESC")},
{"SpectableASC", () => querys.botSpec("ASC")},
{"ParttableDESC", () => querys.TopPart("DESC")},
{"ParttableASC", () => querys.BotPart("ASC")},
{"MantableDESC", () => querys.TopManual("DESC")},
{"MantableASC", () => querys.BotMan("ASC")}, //why is this first to run?????
{"UsersLockedTbl",() => querys.UserLocked()},
{"NewUsersTbl", () => querys.NewUsers()},
{"VRMtotaltblTOP", () => querys.VRMtotalTOP("")},
{"PARTtotaltblTOP", () => querys.PARTtotalTOP("")},
{"SPECtotaltblTOP", () => querys.SPECtotalTOP("")},
{"MANtotaltblTOP", () => querys.MANtotalTOP("")},
{"VRMtotaltblBOT", () => querys.VRMtotalBOT("")},
{"PARTtotaltblBOT", () => querys.PARTtotalBOT("")},
{"SPECtotaltblBOT", () => querys.SPECtotalBOT("")},
{"MANtotaltblBOT", () => querys.MANtotalBOT("")},
};
foreach(var kvp in tables)
{
try
{
MyDataSet.Tables[kvp.Key].Merge(kvp.Value());//error occurs here
SetTxtBox1(String.Format("{0} ==== {1} has been Loaded\r\n", DateTime.Now.ToString(), kvp.Key));
}catch(Exception e)
{
SetTxtBox1(String.Format("{0} ==== {1} has FAILED\r\n", DateTime.Now.ToString(), kvp.Key));
MessageBox.Show("error:::" + e);
}
}
AutoRunApp();
any help would be great.
UPDATE::
heres my query call from the “bunchofquerys” class
public DataTable BotMan(string order)
{
SqlConnection conn = new SqlConnection(myConnString32);
SqlCommand vrm = new SqlCommand();
//BindingSource bindme = new BindingSource();
SqlDataAdapter adapt1 = new SqlDataAdapter(vrm);
// DataSet dataSet1 = new DataSet();
DataTable table1 = new DataTable();
try
{
vrm.Connection = conn;
vrm.CommandType = CommandType.StoredProcedure;
if (order.Equals("DESC"))
{
vrm.CommandText = "dbo.TopManual";
}
vrm.CommandText = "dbo.BotManual";
vrm.CommandTimeout = 360;
vrm.Parameters.AddWithValue("@OrderBy", order);
adapt1.Fill(table1);
return table1;
}
catch (Exception e)
{
MessageBox.Show("eeror::zomg::: " + e);
}
return table1;
}
You said you already have a
DataSet. An easy way to fill all the tables is to useMerge.In this example, I create a mapping between the table names (the name of the table in the
DataSet), and the corresponding query function (assuming that they return aDataTablewith a similar schema, and yourDataSetis namedds).The
Keyis used to query the appropriateDataTablefrom theDataSet, and then theMergemethod is used to “fill” theDataTable.You could do it the other way round and iterate over each table in you
DataSetand get the appropriate loader function via the table’sTableName.Your
try/catchblock can now easily be added to theforeachloop.