Domanda

how to pass ds from sql class to form class

in my form class

sqlCls floor = new sqlCls();
floor.getByFloor(floorNo);
reportFormDataGridView.DataSource = ds.Tables[0]; ****** 

in sql class . floor method

public DataSet getByFloor(int floorNo)
{
    DataSet ds = new DataSet();
    SqlConnection conn = connectionCls.openConnection();
    SqlCommand com = new SqlCommand("select * from table where floorsNo = " + floorNo, conn);

    SqlDataAdapter SE_ADAPTAR = new SqlDataAdapter(com);
    SE_ADAPTAR.Fill(ds);
    conn.Close();

    return ds;
}
È stato utile?

Soluzione

GridViews can take a DataSet as the DataSource just fine, no need to use a table.

Just do this:

sqlCls floor = new sqlCls();
var ds = floor.getByFloor(floorNo);
reportFormDataGridView.DataSource = ds;

You have a SQL Injection vulnerability in your code. Please consider using SQL parameters instead of unsanitized input.

So in your case it would be:

public DataSet getByFloor(int floorNo)
{
    DataSet ds = new DataSet();

    SqlConnection conn = connectionCls.openConnection();
    SqlCommand com = new SqlCommand("select * from table where floorsNo = @floorsNo", conn);
    com.Parameters.AddWithValue("@floorsNo", floorNo);

    using(SqlDataAdapter SE_ADAPTAR = new SqlDataAdapter(com))
    {  
        SE_ADAPTAR.Fill(ds);
        conn.Close();
    }

    return ds;
}

SqlDataAdapter implements the IDisposable interface so you can wrap it in a using block to automatically dispose of resources when execution flow leaves the scope.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top