0
public SqlDataReader GetDataReader(List<SqlParameter> parameterValues){

    System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();
    cn.ConnectionString = SQLConnectionObj.ConnectionString;
    cn.Open();
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.Parameters.AddRange(parameterValues.ToArray());
    cmd.Connection = cn;
    cmd.CommandText = SelectStatement;
    cmd.CommandType = CommandType.Text;
    return sReader = cmd.ExecuteReader(); 
}

When I try to add this for IN condition variable in select query,it fails. Need to use this only for Fortify fix.Tried with dictionary Sql parameter.It works but increases the issue count up.

Please help me with this.And also if there is anything new which you want to add feel free to add those too.

But the following code works:-

   public SqlDataReader GetDataReader(Dictionary<string, string> qParams)
    {
    SqlCommand SQLCommandObj = new SqlCommand(SelectStatement, 
    SQLConnectionObj);
     string query=SelectStatement;

    if (qParams.Count > 0)
    {
        foreach (string key in qParams.Keys)
        {
            string value = qParams[key];
            SqlParameter par = new SqlParameter();
            par.ParameterName = key;
            par.Value = value;
            SQLCommandObj.Parameters.Add(par);
          }
    }

    foreach(SqlParameter par in SQLCommandObj.Parameters)
    {
        string key = par.ParameterName;
        string value = par.Value as string;
        query=query.Replace(key, value);
    }
    if (qParams.Count > 0)
    {
        SQLCommandObj.CommandText = "";
        SQLCommandObj.CommandText = query;
    }

    SQLCommandObj.CommandTimeout = CustomCommandTimeout;
    return SQLCommandObj.ExecuteReader(CommandBehavior.CloseConnection);
}
Umesh Mandal
  • 11
  • 1
  • 3
  • What's in `SelectStatement`? – Zohar Peled Jul 08 '18 at 13:49
  • @ZoharPeled select query is like select *from clothes where material IN (@material) ...and there are also multiple IN conditions in sql statement. – Umesh Mandal Jul 08 '18 at 14:36
  • Possible duplicate of [How to pass string parameter with \`IN\` operator in stored procedure SQL Server 2008](https://stackoverflow.com/questions/16872056/how-to-pass-string-parameter-with-in-operator-in-stored-procedure-sql-server-2) – Peter Hahndorf Jul 08 '18 at 19:23
  • There are some problems with your code - starting with the fact that you don't close or dispose any of the `IDisposable` instances (`SqlConnection`, `SqlCommand`, and `SqlDataReader` are all `IDisposable`). Since you are working with .Net and Sql Server, look into Table valued parameters - in most cases, that's the best practice way to pass an Array like structure to SQL Server. – Zohar Peled Jul 09 '18 at 04:26
  • @ZoharPeled SqlConnection,command and datareader all are there.Just need to use that block fo Fortify.. – Umesh Mandal Jul 13 '18 at 11:54
  • But they are not being disposed... – Zohar Peled Jul 13 '18 at 17:27
  • yeah its all closed and disposed @ZoharPeled – Umesh Mandal Jul 14 '18 at 06:29
  • Not in the codw you've shared – Zohar Peled Jul 14 '18 at 06:40
  • @ZoharPeled its only part of code....full code is really lengthy... – Umesh Mandal Jul 14 '18 at 07:26

0 Answers0