0

I have this situation: in DataEntryForm I have a dropdownlist, where user selects a letter number, and according to that inserts other related data.

I plan to change letter's status in other table by choosing in dropdownlist automatically.

I am using this code:

SqlParameter answertoparam = new SqlParameter("answerto", ansTo);

string commandText = "update IncomeLetters set IncomeLetters.docState_ID ='2' where income_number=('" + ansTo + "' )";

SqlCommand findincomelett = new SqlCommand(commandText, conn);
comm.Parameters.Add(answertoparam);
conn.Open();

findincomelett.ExecuteNonQuery();
comm.ExecuteNonQuery();

Unfortunately, the result is nothing.

Server is not giving error, and it simply refreshes the page that is it.

3 Answers 3

1

In your posted code, you are passing the SqlParameter as well as passing the value as raw data. Do either of one and preferably pass it as SqlParameter like

        SqlParameter answertoparam = new SqlParameter("answertoparam", ansTo);
        string commandText = "update IncomeLetters set IncomeLetters.docState_ID = '2' where income_number = @answertoparam";
        SqlCommand findincomelett = new SqlCommand(commandText, conn);
        findincomelett.Parameters.Add(answertoparam);
        conn.Open();
        findincomelett.ExecuteNonQuery();

Moreover, you have two SqlCommand object in place and calling two ExecuteNonQuery() on them. correct that ... see below

        SqlCommand findincomelett = new SqlCommand(commandText, conn); --1
        comm.Parameters.Add(answertoparam); --2

        conn.Open();

        findincomelett.ExecuteNonQuery(); --1
        comm.ExecuteNonQuery(); --2
Sign up to request clarification or add additional context in comments.

Comments

0

As far as I understand, the issue is that the correct IncomeLetters.docState_ID is not updated to '2'.

You may want to debug and see what value you are getting in :

string ansTo = ddlAnswerTo.SelectedItem.Value;

The record in the database that you are expecting to be updated may not have the record that satisfies the where clause 'income_number = @answertoparam'

Comments

0

I would like to bring you here full code of the page.

Idea is: I have page for enrollment. I am passing data to DB through stored procedure (DataInserter).

Problem is here: during enrollment, user selects from dropdownlist number of the letter he would like to answer to, and in the end, the status of the letter on other table of DB (IncomeLetters.tbl), would change from "pending"('1') to "issued" ('2').

I guess, I could clear my point to you and thank you for your support!

protected void Button1_Click(object sender, EventArgs e)
{
   SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MaktubhoConnectionString2"].ConnectionString);
    using (SqlCommand comm = new SqlCommand("DataInserter", conn))
    {
        comm.CommandType = CommandType.StoredProcedure;
        comm.Connection = conn;
        SqlParameter employeeparam = new SqlParameter("EmployeeSentIndex", int.Parse(ddlemployee.SelectedItem.Value));
        SqlParameter doctypeparam = new SqlParameter("doctype_ID", int.Parse(ddldoctype.SelectedItem.Value));
        SqlParameter doccharparam = new SqlParameter("docchar_ID", int.Parse(ddldocchar.SelectedItem.Value));
        SqlParameter authorityparam = new SqlParameter("authority", txtauthority.Text);
        SqlParameter subjectparam = new SqlParameter("subject", txtsubject.Text);

        DateTime dt = DateTime.Now;
        string todasdate = dt.ToString("d", CultureInfo.CreateSpecificCulture("de-DE"));
        SqlParameter entrydateparam = new SqlParameter("entrydate", todasdate);

        string Pathname = "UploadImages/" + Path.GetFileName(FileUpload1.PostedFile.FileName);
        SqlParameter imagepathparam = new SqlParameter("image_path",  Pathname);
        SqlParameter loginparam = new SqlParameter("login", "jsomon");

        comm.Parameters.Add(employeeparam);
        comm.Parameters.Add(doctypeparam);
        comm.Parameters.Add(doccharparam);
        comm.Parameters.Add(authorityparam);
        comm.Parameters.Add(subjectparam);
        comm.Parameters.Add(entrydateparam);
        comm.Parameters.Add(imagepathparam);
        comm.Parameters.Add(loginparam);
        comm.Parameters.Add("@forlabel", SqlDbType.VarChar, 100);
        comm.Parameters["@forlabel"].Direction = ParameterDirection.Output;
        FileUpload1.SaveAs(Server.MapPath("~/UploadImages/" + FileUpload1.FileName));
        string ansTo = ddlAnswerTo.SelectedItem.Value;
        SqlParameter answertoparam = new SqlParameter("answertoparam", ansTo);
        string commandText = "update IncomeLetters set IncomeLetters.docState_ID = '2' where income_number = @answertoparam";
        SqlCommand findincomelett = new SqlCommand(commandText, conn);
        findincomelett.Parameters.Add(answertoparam);
            conn.Open();
            findincomelett.ExecuteNonQuery();
            comm.ExecuteNonQuery();
            lblresult.Visible = true;
            Image1.Visible = true;
            lblresult.Text = "Document number:";
            lblnumber.Visible = true;
            lblnumber.Text = (string)comm.Parameters["@forlabel"].Value; ;
            conn.Close();
        }

    txtauthority.Text = "";
    txtsubject.Text = "";
}

1 Comment

If you need to amend your question, EDIT your question - don't post a dummy answer to your own question.....

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.