0

I am trying to execute a stored procedure in asp.net. The stored procedure requires 3 parameters, all 3 are ID's(ints). The 3 parameters are : TaskID, ExhibitID, and InvestigatorID.

I have a hidden field that contains an array of ExhibitID's that came from a javascript function. My question is how do I get the query to execute as I am looping through the array?

Here is an example of my stored procedure:

var cnSaveTask = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
        var comLinkExhibitToTask = new SqlCommand("p_CaseFileTasksExhibitLinkAdd", cnSaveTask) { CommandType = CommandType.StoredProcedure };
        foreach (string exhibit in hidExhibitsIDs.Value.Split(','))
        {
            comLinkExhibitToTask.Parameters.AddWithValue("@TaskID", taskID);
            comLinkExhibitToTask.Parameters.AddWithValue("@ExhibitID", Convert.ToInt32(exhibit));
            comLinkExhibitToTask.Parameters.AddWithValue("@InvestigatorID", int.Parse(Session["InvestigatorID"].ToString()));

        }

        try
        {
            cnSaveTask.Open();
            comLinkExhibitToTask.ExecuteNonQuery();
        }

It is not working in my DB though. Nothing gets added. My guess is that since it is iterating and not executing, it just keeps replacing the "exhibitID" everytime then eventually tries to execute it. But I don't think just adding "comLinkExhibitToTask.ExecuteNonQuery()" outside the try is a good idea. Any suggestions?

0

3 Answers 3

1

you can either move the try block into the foreach loop or wrap the foreach loop with a try block. (depending on what error handling you wish - continue with the next exhibit on error or completely abort execution)

Sign up to request clarification or add additional context in comments.

Comments

1

I've never used AddWithValue, so I can't speak to its functionality. Here's how I typically write a DB call like this.

using (SqlConnection cnSaveTask = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ConnectionString))
{
    cnSaveTask.Open();

    using (SqlCommand comLinkExhibitToTask = new SqlCommand("p_CaseFileTasksExhibitLinkAdd", cnSaveTask))
    {
        comLinkExhibitToTask.CommandType = CommandType.StoredProcedure;

        comLinkExhibitToTask.Parameters.Add(new SqlParameter("@TaskID", SqlDbType.Int) {Value = taskID});
        // etc.

        comLinkExhibitToTask.ExecuteNonQuery();
    }
}

2 Comments

I think "AddWithValue" is causing the problem, because I get a "storedProcedureName has too many arguments". I think it just keeps adding parameters.
Will let you know in a few mins. Gotta recompile
0

The solution:

var cnSaveTask = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
        try
        {
            var comLinkExhibitToTask = new SqlCommand("p_CaseFileTasksExhibitLinkAdd", cnSaveTask) { CommandType = CommandType.StoredProcedure };
            cnSaveTask.Open();
            comLinkExhibitToTask.Parameters.Add(new SqlParameter("@TaskID", SqlDbType.Int));
            comLinkExhibitToTask.Parameters.Add(new SqlParameter("@ExhibitID", SqlDbType.Int));
            comLinkExhibitToTask.Parameters.Add(new SqlParameter("@InvestigatorID", SqlDbType.Int));

            foreach (string exhibit in hidExhibitsIDs.Value.Split(','))
            {
                comLinkExhibitToTask.Parameters["@TaskID"].Value = taskID;
                comLinkExhibitToTask.Parameters["@ExhibitID"].Value = Convert.ToInt32(exhibit);
                comLinkExhibitToTask.Parameters["@InvestigatorID"].Value = int.Parse(Session["InvestigatorID"].ToString());

                comLinkExhibitToTask.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            ErrorLogger.Log(0, ex.Source, ex.Message);
        }
        finally
        {
            if (cnSaveTask.State == ConnectionState.Open)
            {
                cnSaveTask.Close();
            }
        }

Since I was in a loop it kept adding parameters. So just declare the parameters outside the loop, and only pass the values in the loop. That way there are only 3 parameters, and the values will be passed in accordingly

1 Comment

Consider the "using" statement as well. It will handle your finally block automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.