Click here to Skip to main content
16,009,640 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello World..
I have a problem when I save data in SQL (2008r2 database)using windows form application in "C# programming language",
My code in the save button is right ,but when I click on this button ,messagebox appear (Invalid column name "CompanyID"...and other column),but the column is existing in the same table in database.

My code is
C#
SqlConnection con = new SqlConnection(GetConnection.ConnectionStr);
string sqltext = "insert into MVConsumingMaker ( [CompanyID] , [MakerID] , [Hour] , [Amount] , [Date] ) values(" + Convert.ToString(CmpCompany.ValueMember) + "," + Convert.ToString(CmpMaker.ValueMember) + "," + CmpHour.Text + "," + TxtAmount.Text + ",'" + MaskDate.Text + "')";
SqlCommand cmd = new SqlCommand(sqltext, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
TxtResultSave.Text = "the opiration was completed successfully";

please till me , what I can doing now!
thank you..
Posted

1. Try it without the [] brackets around the field names:

SQL
insert into MVConsumingMaker ( CompanyID , MakerID , Hour , Amount , Date ) values(...)


2. Verify that the spelling and capitalization matches. For example, make sure it's CompanyID in the table, and not CompanyId or company_id or whatever....
 
Share this answer
 
Comments
anas-123 12-Apr-12 16:15pm    
I tryout all you sayed before sometimes,all that unavailing,and I can't know waht the problem
Here is my theory. The credentials you are logging on to SQL Server are not taking you the schema you think you should go to. Whatever schema you are connecting using your .Net code has the table MVConsumingMaker, but not column CompanyID.

For example: When you connect using SQL Studio you may be looking at dbo.MVConsumingMaker, however your .Net code may be trying to access user1.MVConsumingMaker because the .Net connection connects to user1 schema.

SQL Server is case insensitive so discard the possibility of bad capitalization.

I have nothing against using the insert statement from C# though most people would have used a stored procedure. I have problem against creating a dynamic SQL string within C#. In your query, you are lucky that there are no varchar columns. Let us say you did, you will go nuts dealing with quotes within your data.
 
Share this answer
 
Comments
anas-123 13-Apr-12 14:33pm    
thank you,that is not solve to my problem,in the end I explore the solve, I must use the property:selectedvalue,for compobox instead valuemember.

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