Good morning Wim,
I hope that I am not confusing. I am not trying to say your original piece of code is not working. I am debugging the code and I am visually seeing the code is crashing. I am providing the complete function in order to have a big picture . It should populate the data based on the parameters that I am sending from the code behind.
protected bool GetData(DateTime start, DateTime end, string campaign) { bool flag = false; OleDbConnection connection = null; OleDbCommand selectCommand = null; OleDbDataAdapter adapter = null; DataSet dataSet = new DataSet(); //DataSet dataSet = null; string str = base.Request.QueryString["OB"]; string str2 = base.Request.QueryString["Alias"]; string cObAlias = ""; if (string.IsNullOrEmpty(str)) { str = ConfigurationManager.AppSettings["ObjectBroker"]; } string obConnectionString = AmAccess.GetObConnectionString(str, ref cObAlias); try { connection = new OleDbConnection(obConnectionString); connection.Open(); selectCommand = new OleDbCommand("clients.dbo.spx_AmountStatsReport11", connection) { CommandType = CommandType.StoredProcedure, CommandTimeout = connection.ConnectionTimeout }; selectCommand.Parameters.AddWithValue("@start", start); selectCommand.Parameters.AddWithValue("@end", end); if (string.IsNullOrEmpty(campaign)) { if (this.ddlCampaigns.SelectedItem.Text == "All") { selectCommand.Parameters.AddWithValue("@campaignId", DBNull.Value); selectCommand.Parameters.AddWithValue("@campaign", DBNull.Value); selectCommand.Parameters.AddWithValue("@segment", DBNull.Value); selectCommand.Parameters.AddWithValue("@downloadDescriptions", DBNull.Value); } else { string s = "@segment"; // either s = "@segment = " or s = "@segment"// both does not work // get selected segments int[] selectedsegments = lbSegments.GetSelectedIndices(); // add selected segments to s for (int cnt = 0; cnt < selectedsegments.Length; cnt++) { s += lbSegments.Items[selectedsegments[cnt]].Text + ","; // by finishing this line, codes will crash immediately. } // display
selectCommand.Parameters.AddWithValue("@campaignId", this.ddlCampaigns.SelectedValue); selectCommand.Parameters.AddWithValue("@campaign", DBNull.Value); selectCommand.Parameters.AddWithValue("@segment", s); } } else if (campaign == "All") { selectCommand.Parameters.AddWithValue("@campaignId", DBNull.Value); selectCommand.Parameters.AddWithValue("@campaign", DBNull.Value); selectCommand.Parameters.AddWithValue("@segment", DBNull.Value); selectCommand.Parameters.AddWithValue("@downloadDescriptions", DBNull.Value); } else { selectCommand.Parameters.AddWithValue("@campaignId", DBNull.Value); selectCommand.Parameters.AddWithValue("@campaign", campaign); selectCommand.Parameters.AddWithValue("@segment", DBNull.Value); selectCommand.Parameters.AddWithValue("@downloadDescriptions", DBNull.Value); } adapter = new OleDbDataAdapter(selectCommand); //dataSet = new DataSet(); adapter.Fill(dataSet); if (dataSet.Tables[0].Rows.Count > 0) { this.m_Data = "1"; flag = true; } if (this.CheckConfigurationOn("RemoveTotalRows")) { if (dataSet.Tables[0].Rows.Count > 0) { dataSet.Tables[0].Rows[dataSet.Tables[0].Rows.Count - 1].Delete(); } if (dataSet.Tables[1].Rows.Count > 0) { dataSet.Tables[1].Rows[dataSet.Tables[1].Rows.Count - 1].Delete(); } } this.gvSummary.DataSource = dataSet.Tables[0]; this.gvSummary.DataBind(); this.GridView1.DataSource = dataSet.Tables[1]; this.GridView1.DataBind(); this.SaveColumnNames(this.gvColumnNames, dataSet.Tables[1]); this.SaveColumnNames(this.gvColumnNamesSummary, dataSet.Tables[0]); this.SetRefreshTimer(); } catch (Exception exception) { this.m_Error = exception.Message; this.m_Error = this.m_Error + exception.StackTrace; this.m_Error = this.m_Error.Replace(@"\", @"\\"); this.m_Error = this.m_Error.Replace("'", @"\'"); this.m_Error = Regex.Replace(this.m_Error, @"\s+", " ").Trim(); this.m_Error = base.Server.HtmlEncode(this.m_Error); } finally { dataSet.Dispose(); dataSet = null; adapter.Dispose(); adapter = null; selectCommand.Dispose(); selectCommand = null; connection.Close(); connection = null; } return flag; }
now, if I use the same function above with little bit modification to your original code, it will work only if I select a single segment. it generates the report but it will fail if I select multiple segments. it crashes.
Here is the modification codes //string s = "@segment"; string @segment = ""; // get selected segments int[] selectedsegments = lbSegments.GetSelectedIndices(); // add selected segments to s for (int cnt = 0; cnt < selectedsegments.Length; cnt++) { @segment += lbSegments.Items[selectedsegments[cnt]].Text + ","; } // display lbSegments.Text = @segment.Trim(','); selectCommand.Parameters.AddWithValue("@campaignId", this.ddlCampaigns.SelectedValue); selectCommand.Parameters.AddWithValue("@campaign", DBNull.Value); selectCommand.Parameters.AddWithValue(@segment, lbSegments.SelectedValue);
I was actually debugging it and I was stepping through each step and I saw exactly where it crashed. Hopefully this will make sense to you. My question is if it is working for a single select why it is crashing for multiple selects. what things should be changed to make it runs. I forgot to mention. I have changed the dataset from null to new dataset, I put it in bold in case you want to see their location.