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);
}
}
}
}
Piyush PansuriyaRank #454 · 5.5K reputationPosted Oct 25, 2018, 4:35 AM EDT
Udit ChauhanRank #3,035 · 475 reputationPosted Oct 25, 2018, 3:51 AM EDT
Scott G111 reputationPosted Oct 25, 2018, 4:48 AM EDT
Udit ChauhanRank #3,035 · 475 reputationPosted Oct 25, 2018, 5:32 AM EDT
Scott G111 reputationPosted Oct 25, 2018, 5:58 AM EDT
Udit ChauhanRank #3,035 · 475 reputationPosted Oct 25, 2018, 6:38 AM EDT
Scott G111 reputationPosted Oct 25, 2018, 6:51 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts