Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, i'm trying to put a 2 select in a store proc then call it in c# to lessen my code

Here is my store proc
ELSE IF(@Event = 'getOperatorInfo')
				BEGIN
					SELECT operatorCode,operatorClient,operatorName,Expiration
					FROM codecoOperatorInfo
					WHERE operatorCode = @operatorCode AND operatorClient = @operatorClient

					SELECT operatorRecipients
					FROM codecoOperatorsRecipients
					WHERE operatorCode = @operatorCode AND operatorClient = @operatorClient
				END


and here is my c# code

public List<operatorSettingsDO> getOperatorInfo(string Code, string Client)
        {
            var oOperatorSettingsDO = new List<operatorSettingsDO>();
            using (SqlConnection oConnection = new SqlConnection(sqlConnection))
            {
                using (SqlCommand oCommand = new SqlCommand("spCodeco", oConnection))
                {
                    oCommand.Connection = oConnection;
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.AddWithValue("@Form", "operatorSettings");
                    oCommand.Parameters.AddWithValue("@Event", "getOperatorInfo");
                    oCommand.Parameters.AddWithValue("@operatorCode", Code);
                    oCommand.Parameters.AddWithValue("@operatorClient", Client);
                    SqlDataReader oReader = null;
                    try
                    {
                        oConnection.Open();
                        oReader = oCommand.ExecuteReader();
                        while (oReader.Read())
                        {
                            operatorSettingsDO opSettingsDO = new operatorSettingsDO();
                            opSettingsDO.operatorCode = oReader["operatorCode"].ToString();
                            opSettingsDO.operatorClient = oReader["operatorClient"].ToString();
                            opSettingsDO.operatorName = oReader["operatorName"].ToString();
                            opSettingsDO.Expiration = (DateTime)oReader["Expiration"];


                            opSettingsDO.Recipients = oReader["operatorRecipients"].ToString();
                            oOperatorSettingsDO.Add(opSettingsDO);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            return oOperatorSettingsDO;
        }


it says System.IndexOutOfRangeException "operatorRecipients"

What I have tried:

i'm still trying to figure out if this can be used or else I'll stick to to a longer code.
Posted
Updated 22-Jun-20 5:23am
Comments
F-ES Sitecore 22-Jun-20 11:12am    
Your code returns two result selects, so your ".Read" is going through the first result row by row and operatorRecipients isn't in that so you can't read it. Once you have completed the first result set you then need to move onto the next result set and process that and your operatorRecipients will be available there.
Richard Deeming 25-Jun-20 13:25pm    
catch (Exception ex)
{
    throw ex;
}

Don't do that. You've just thrown away the stack trace of the exception, making it much harder to track down the source of the error.

If you really want to re-throw an exception, use throw; instead of throw ex;
catch (Exception ex)
{
    throw;
}

But in this case, since you're not doing anything with the exception once you've caught it, you might as well remove the try..catch block completely.

C#
var result = new DataSet();
        var dataAdapter = new SqlDataAdapter(command);
        dataAdapter.Fill(result);


At that point, you can reference each result set in different tables inside the dataset...

result.Tables[0] and result.Tables[1]
 
Share this answer
 
The quickest way to do this is to use an SQL Data Adapter; which in turn will create a DataSet containing one DataTable per resultset coming back from your procedure
Multiple Result Sets

If the DataAdapter encounters multiple result sets, it creates multiple tables in the DataSet. The tables are given an incremental default name of TableN, starting with "Table" for Table0. If a table name is passed as an argument to the Fill method, the tables are given an incremental default name of TableNameN, starting with "TableName" for TableName0.
References:
SqlDataAdapter Class (System.Data.SqlClient) | Microsoft Docs[^]
Populating a DataSet from a DataAdapter - ADO.NET | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900