Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Add Parameters to stored procedure inside a loop
    primarykey
    data
    text
    <p>I have been trying to add parameters to a stored procedure inside a loop. Below given is the code where I declared my variables.</p> <pre><code> SqlConnection con = new SqlConnection(); Connect conn = new Connect(); SqlDataReader readerCourseID = null; con = conn.getConnected(); con.Open(); SqlCommand cmdAssignCourse; cmdAssignCourse = new SqlCommand("assignCourse", con); cmdAssignCourse.CommandType = CommandType.StoredProcedure; cmdAssignCourse.Parameters.Add("@sID", System.Data.SqlDbType.VarChar); cmdAssignCourse.Parameters.Add("@cID", System.Data.SqlDbType.VarChar); SqlParameter retValue = cmdAssignCourse.Parameters.Add("return", System.Data.SqlDbType.Int); </code></pre> <p>And below is the code where I inserted values into the variables that were declared earlier.</p> <pre><code>foreach (DataRow row in dt.Rows) { //get course id from course name. Pass row["Course Name"].ToString() int i = getCourseID(row["Course Name"].ToString()); //assignment of the course to student cmdAssignCourse.Parameters["@sID"].Value = studentCurrID.Value.ToString(); cmdAssignCourse.Parameters["@cID"].Value = i; retValue.Direction = ParameterDirection.ReturnValue; cmdAssignCourse.ExecuteNonQuery(); if (retValue.Value.ToString() == "0") { MessageBox.Show("Added Course Successfully!"); //return 0; } else { MessageBox.Show("An error occured! Possibly a duplication of data!"); //return -1; } } </code></pre> <p>However this code runs successfully and displays the message "Added Course Successfully!" once. But after the first successful run every other run it gives me "An error occured! Possibly a duplication of data!" message. The possible error is not clearing out the variables. How to clear the below variables. Please help me with this. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload