Hello and good day to all. I have an issue on coding, i have long line of codes just to show 16 items from sql server to 16 textblocks, there is no error but i want to maintain the short line of codes. Here is 2 textblocks(out of 16) example codes:
orgDa.SelectCommand = conn.CreateCommand();
orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=1";
orgDa.SelectCommand.CommandType = CommandType.Text;
orgDa.Fill(ds, "Organizationtbl");
deptDa.SelectCommand = conn.CreateCommand();
deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=1";
deptDa.SelectCommand.CommandType = CommandType.Text;
deptDa.Fill(ds, "Departmenttbl");
if (ds.Tables["Organizationtbl"].Rows.Count == 1)
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
if (orgItem.IsNull("OrganizationName"))
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString();
}
}
else
{
textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString();
}
}
}
else
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
if (deptItem.IsNull("DepartmentName"))
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString();
}
}
else
{
textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString();
}
}
}
orgDa.SelectCommand = conn.CreateCommand();
orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=2";
orgDa.SelectCommand.CommandType = CommandType.Text;
orgDa.Fill(ds, "Organizationtbl");
deptDa.SelectCommand = conn.CreateCommand();
deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=2";
deptDa.SelectCommand.CommandType = CommandType.Text;
deptDa.Fill(ds, "Departmenttbl");
if (ds.Tables["Organizationtbl"].Rows.Count == 1)
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
if (orgItem.IsNull("OrganizationName"))
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString();
}
}
else
{
textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString();
}
}
}
else
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
if (deptItem.IsNull("DepartmentName"))
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString();
}
}
else
{
textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString();
}
}
}
How can i show 16 items from sql to 16 textblocks in just one loop? i need your help. Thank you in advance.
Generally, if you have code that is repeated, try to do the following:
Take the code for one item, move it into a function.
Find everything that differs for each item: like queries, table names, column names, controls etc.. Add a parameter to the method for each of them and replace each occurrence in the code with the parameter.
Invoke the method one time for each item, passing in the right parameters.
It’s basically a two step refactoring: first, extract method. Second, extract parameter.
However, in your case, mainly the OrgId and DeptID seem to change (and the control to assign the value to), so read about parameterized queries, e.g. http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
Your extracted method could look like this (not tested, nor compiled):
And then call it with:
or with some smarter loop.
This could ca be simplified even more, of course. There seems to be a lot of similarities in the two branches of the if-else construct.