How could I change this piece of code to a function with parameters?
My variables are:
- ClientsTbl
- ClCode
- gridView
- Tables.ClientsTbl
- txtbx
Code:
string sql = "SELECT *";
sql += "FROM ClientsTbl ";
sql += " Order By ClCode";
gridView.AutoGenerateColumns = true;
string jsonTbl = GetJsonFromSql(sql);
List<db.Tables.ClientsTbl> localsal = JsonConvert.DeserializeObject<List<db.Tables.ClientsTbl>>(jsonTbl);
gridView.DataSource = localsal;
txtbx.Text = (from DataGridViewRow row in gridView.Rows
where !String.IsNullOrEmpty(row.Cells[1].FormattedValue.ToString())
select Convert.ToDouble(row.Cells[1].FormattedValue)).Count().ToString();
txtbx.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(txtbx.Text));
I trying to use this code:
public static void gvLoad (string tbl, string pk, DataGridView gridView, TextBox txtbx)
{
string sql = "SELECT *";
sql += "FROM " + tbl + "";
sql += " Order By " + pk + "";
gridView.AutoGenerateColumns = true;
string jsonTbl = GetJsonFromSql(sql);
List<db.Tables.ClientsTbl> localsal = JsonConvert.DeserializeObject<List<db.Tables.ClientsTbl>>(jsonTbl);
gridView.DataSource = localsal;
txtbx.Text = (from DataGridViewRow row in gridView.Rows
where !String.IsNullOrEmpty(row.Cells[1].FormattedValue.ToString())
select Convert.ToDouble(row.Cells[1].FormattedValue)).Count().ToString();
txtbx.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(txtbx.Text));
}
but I don’t know how to make parameter for Tables.ClientsTbl
.
Any help please?
2
Answers
To make the
Tables.ClientsTbl
a parameter in thegvLoad
function, you can pass it as a generic type parameter. Here's an updated version of the function:In this version, the type parameter,
T
, is used to represent the type of the table, which should match the type of theTables.ClientsTbl
object. You can call the function like this:This will pass the
Tables.ClientsTbl
type as the generic parameterT
, allowing the function to deserialize the JSON into a list ofdb.Tables.ClientsTbl
objects.Maybe you can use the stored procedure to concat and execute sql dynamicly if you are using the Microsoft SQL Server database.
for example:
or you can use EF + Linq + some package like mapper