Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
im trying to update a table with two composite primary keys but it wont update.

What I have tried:

public void update(MOduleQuestion moduleq)
        {
            string mcode = moduleq.getModuleCode();
            int QNo = moduleq.getQuestionNo();
            string Question = moduleq.getQuestion();
            string OPt1 = moduleq.getOP1();
            string OPt2 = moduleq.getOP2();
            string OPt3 = moduleq.getOP3();
            string OPt4 = moduleq.getOP4();
            string Answer = moduleq.getAnswer();


            try
            {
                //string sql = "Update Module_Question SET Question='" + Question + "',Option1='" + OPt1 + "',Option2='" + OPt2 + "',Option3='" + OPt3 + "',Option4='" + OPt4 + "' WHERE MQ_Module_Code = '" + mcode + "' AND MQ_Module_No= '" + QNo + "' ";
                string sql = "Update Module_Question SET Question = @question, OPt1 = @opt1, OPt2 = @opt2, OPt3 =  @opt3, OPt4 = @opt4, Answer = @answer WHERE MQ_Module_Code = @mcode and  MQ_Question_No  = @qno";
                SqlCommand cmd = new SqlCommand(sql, m_con);
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@mcode", mcode);
                cmd.Parameters.AddWithValue("@qno", QNo);
                cmd.Parameters.AddWithValue("@question", Question);
                cmd.Parameters.AddWithValue("@opt1", OPt1);
                cmd.Parameters.AddWithValue("@opt2", OPt2);
                cmd.Parameters.AddWithValue("@opt3", OPt3);
                cmd.Parameters.AddWithValue("@opt4", OPt4);
                cmd.Parameters.AddWithValue("@answer", Answer);

                //SqlCommand cmd = new SqlCommand(sql, m_con);
                m_con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Question Record Updated Successfully", "lecture Reg System", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SqlException)
            {
                MessageBox.Show("Cannot Update", "lecture Reg System", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            finally
            {
                m_con.Close(); // connection close


            }
        }
Posted
Updated 14-Mar-17 8:09am
v4
Comments
[no name] 14-Mar-17 12:53pm    
You want AddWithValue for your parameters
CHill60 14-Mar-17 13:05pm    
Yep - that'll do it.
Member 13049972 14-Mar-17 13:22pm    
so i have to type AddWithValue Module_Question SET Question =.... lie this?
[no name] 14-Mar-17 13:42pm    
No.... cmd.Parameters.AddWithValue("@mcode", mcode);

You can learn a lot from reading the documentation, just saying.
Karthik_Mahalingam 14-Mar-17 13:30pm    
are you getting any error?

1 solution

//Update a lecture
        public void update(MOduleQuestion moduleq)
        {
            string mcode = moduleq.getModuleCode();
            int QNo = moduleq.getQuestionNo();
            string Question = moduleq.getQuestion();
            string OPt1 = moduleq.getOP1();
            string OPt2 = moduleq.getOP2();
            string OPt3 = moduleq.getOP3();
            string OPt4 = moduleq.getOP4();
            string Answer = moduleq.getAnswer();


            try
            {
                //string sql = "Update Module_Question SET Question='" + Question + "',Option1='" + OPt1 + "',Option2='" + OPt2 + "',Option3='" + OPt3 + "',Option4='" + OPt4 + "' WHERE MQ_Module_Code = '" + mcode + "' AND MQ_Module_No= '" + QNo + "' ";
                string sql = "Update Module_Question SET Question = @question, Option1 = @opt1, Option2 = @opt2, Option3 =  @opt3, Option4 = @opt4, Answer = @answer WHERE MQ_Module_Code = @mcode and  MQ_Question_No  = @qno";
                SqlCommand cmd = new SqlCommand(sql, m_con);
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@mcode", mcode);
                cmd.Parameters.AddWithValue("@qno", QNo);
                cmd.Parameters.AddWithValue("@question", Question);
                cmd.Parameters.AddWithValue("@opt1", OPt1);
                cmd.Parameters.AddWithValue("@opt2", OPt2);
                cmd.Parameters.AddWithValue("@opt3", OPt3);
                cmd.Parameters.AddWithValue("@opt4", OPt4);
                cmd.Parameters.AddWithValue("@answer", Answer);

                //SqlCommand cmd = new SqlCommand(sql, m_con);
                m_con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Question Record Updated Successfully", "lecture Reg System", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                m_con.Close(); // connection close


            }
        }
 
Share this answer
 
Comments
Member 13049972 14-Mar-17 14:11pm    
This solution i created after help of @Karthik Bangalore

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900