Scott G

Scott G

  • NA
  • 103
  • 7.7k

Stored Procedure will not execute on second button click

Oct 25 2018 7:34 AM
I have a stored procedure that executes a search when a button is clicked in my appplication. Two parameters are passed to the procedure and all works great the first time the procedure is executed. But if I change the search parameter and string and try to execute it again it doesn't work. I was getting an error that I was sending "too many arguments specified". I realized I wasn't clearing my parameters before executing the sp again. so I added a clear statement and now it will not run at all on the second click. Any suggestions?
 
 
public partial class InitialDebt : Form
{
static string conn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString;
static SqlConnection misc = new SqlConnection(conn);
static SqlCommand initDebt = misc.CreateCommand();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(initDebt);
static int index = 0;
private void btnSearch_Click(object sender, EventArgs e)
{
if (ddlFilter.Text == "SELECT")
{
MessageBox.Show("Please select a value from the drop down list");
}
else
{
try
{
initDebt.CommandType = CommandType.StoredProcedure;
initDebt.CommandText = "MyStoredProc";
initDebt.Parameters.Clear();
initDebt.Parameters.Add("@Filter", SqlDbType.VarChar, 20).Value = ddlFilter.Text;
initDebt.Parameters.Add("@SearchStr", SqlDbType.VarChar, 40).Value = txtSearchStr.Text;
da.Fill(dt);
txtSS.Text = dt.Rows[0]["SS_Num"].ToString();
txtLName.Text = dt.Rows[0]["last_Name"].ToString();
txtFName.Text = dt.Rows[0]["first_name"].ToString();
txtAgt.Text = dt.Rows[0]["agent_num"].ToString();
txtStore.Text = dt.Rows[0]["agent_name"].ToString();
}
catch (Exception ex)
{
MessageBox.Show("There are no records that match your search criteria. "+ ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
 

Answers (7)