Click here to Skip to main content
15,902,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two winforms where form 1 is used to enter data from first seven fields and other forms is used to enter for last three fields , where i have the table stored in MSSQL server ,Now when i try to save the last three fields i am getting below " invalid column error , but actually it is present in my sql table , i am very new to C# , kindly help me :

https://i.stack.imgur.com/2Obur.png[^]

https://i.stack.imgur.com/PwI4y.png[^]

What I have tried:

Code for saving data in second form:

private void Btnrejsave_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd;
            con.Open();
            string s = "select * from RejReason where SpoolId = (@p4) insert into IP_Spools(RejectReason1,RejectReason2,RejectReason3) values(@p1,@p2,@p3)";
            cmd = new SqlCommand(s, con);
            cmd.Parameters.AddWithValue("@p1", rejectReason1ComboBox.Text);
            cmd.Parameters.AddWithValue("@p2", rejectReason2ComboBox.Text);
            cmd.Parameters.AddWithValue("@p3", rejectReason3ComboBox.Text);
            cmd.Parameters.AddWithValue("@p4", IDtextbox.Text);
            cmd.CommandType = CommandType.Text;
            int i = cmd.ExecuteNonQuery();
            con.Close();

        }
Posted
Updated 11-Dec-20 20:27pm

1 solution

Why are you mixing a SELECT with an INSERT and calling ExecuteNonQuery?

Start by streamlining your query:
string s = "INSERT INTO IP_Spools (RejectReason1,RejectReason2,RejectReason3) VALUES (@p1,@p2,@p3)";
And remove the fourth parameter - I suspect the error will go away because SpoolID is not a column in the RejReason table.
 
Share this answer
 
Comments
Member 14898617 12-Dec-20 3:03am    
Hi ..Thanks for your comment but now the data can't be saved because the data in the first form is adding as a separate row , and the data in this form is adding as a separate row , please help..
OriginalGriff 12-Dec-20 3:37am    
If you INSERT data then it's always added as a new row, that's what INSERT is for!
TO add information to an existing row, you use an UPDATE command instead.
Member 14898617 12-Dec-20 3:40am    
Thank you for the suggestion ,i'll try the same
OriginalGriff 12-Dec-20 3:53am    
You're welcome!

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