System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'

  • Thread starter Thread starter Tamer Galal
  • Start date Start date
T

Tamer Galal

Guest
Message Error is ( System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index')

Pleas Help

the Code is:

public void RetrieveUserPermissionByRole(String Role, Form Form)
{
dbhelper.OpenDBConnection();
string query = "SELECT Users_tbl.user_role, Permissions_tbl.per_id, Permissions_tbl.per_name, Permissions_tbl.per_form, RolePermissions_tbl.role_code, RolePermissions_tbl.per_id, RolePermissions_tbl.user_id FROM Users_tbl, Permissions_tbl INNER JOIN RolePermissions_tbl ON Permissions_tbl.per_id = RolePermissions_tbl.per_id WHERE Permissions_tbl.per_form = '"+Form.Name+"' AND RolePermissions_tbl.role_code = '"+Role+"' ";
SqlCommand cmd = new SqlCommand(query,dbhelper.GetDbConnection());

//create data Adapter
DataTable datatable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
// this will query your database and return the result to your data

da.Fill(datatable);
da.Dispose();
dbhelper.CloseDbConnection();

//MessageBox.Show(datatable.Rows.Count.ToString());

for (int i= 0; i < datatable.Rows.Count; i++)
{
foreach (var MS in Form.Controls.OfType<MenuStrip>())
{
if (MS.Items.Name == datatable.Rows[1].ToString())
{
if (datatable.Rows[5].ToString().Equals("1"))
{
MS.Items.Enabled = true;
}
else if (datatable.Rows[5].ToString().Equals("0"))
{
MS.Items.Enabled = false;
}
}
}

foreach (var btn in Form.Controls.OfType<Button>())
{
if (btn.Name == datatable.Rows[1].ToString())
{
if (datatable.Rows[5].ToString().Equals("1"))
{
btn.Enabled = true;
}
else if (datatable.Rows[5].ToString().Equals("0"))
{
btn.Enabled = false;
}
}
}
}
}

Continue reading...
 
Back
Top