skip to Main Content

How could I change this piece of code to a function with parameters?

My variables are:

  1. ClientsTbl
  2. ClCode
  3. gridView
  4. Tables.ClientsTbl
  5. 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


  1. Chosen as BEST ANSWER

    To make the Tables.ClientsTbl a parameter in the gvLoad function, you can pass it as a generic type parameter. Here's an updated version of the function:

    public static void gvLoad<T>(string tbl, string pk, DataGridView gridView, TextBox txtbx)
    {
        string sql = $"SELECT * FROM {tbl} ORDER BY {pk}";
    
        gridView.AutoGenerateColumns = true;
        string jsonTbl = GetJsonFromSql(sql);
        List<T> localsal = JsonConvert.DeserializeObject<List<T>>(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));
    }
    

    In this version, the type parameter, T, is used to represent the type of the table, which should match the type of the Tables.ClientsTbl object. You can call the function like this:

    gvLoad<db.Tables.ClientsTbl>("ClientsTbl", "ClCode", myGridView, myTextBox);
    

    This will pass the Tables.ClientsTbl type as the generic parameter T, allowing the function to deserialize the JSON into a list of db.Tables.ClientsTbl objects.


  2. Maybe you can use the stored procedure to concat and execute sql dynamicly if you are using the Microsoft SQL Server database.
    for example:

    DECLARE @Columns VARCHAR(MAX)
    DECLARE @TblName VARCHAR(50)
    DECLARE @SortKey VARCHAR(50)
    DECLARE @SortType BIT = 0
    -- other parameters definition
    DECLARE @sql NVARCHAR(MAX)
    
    SET @sql = 'SELECT ' + @Columns + ' FROM ' + QUOTENAME(@TblName) + ' WHERE 1=1 ' -- replace 1=1 to your filter condition
    IF @SortType = 0
      BEGIN
       SET @sql = CONCAT(@sql, ' ORDER BY ' + @SortKey + 'ASC')
      END
    ELSE
      BEGIN
       SET @sql = CONCAT(@sql, ' ORDER BY ' + @SortKey + 'DESC')
      END
    EXEC sp_executesql @sql
    

    or you can use EF + Linq + some package like mapper

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search