Click here to Skip to main content
14,664,481 members
Rate this:
Please Sign up or sign in to vote.
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
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);
TxtResultSave.Text = "the opiration was completed successfully";

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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....
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100