Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
morning guys my problem is that i keep getting a eroor saying staff1 does not exist but infact it does , here is my c# code:

C#
SqlCommand cmd = new SqlCommand("INSERT INTO staff1 (name ........ect ect ect)


my sql server 2012 query:

SQL
create database staff

use staff

create table staff1(
id int primary key identity,
name         varchar(50),
surname      varchar(50),
homead       varchar(50),
location     varchar(50),
position     varchar(50),
cellnum     varchar(50),
email        varchar(50),
additionalcontact  varchar(50),
additionalcontactnumb  varchar(50));
Posted
Updated 2-Aug-15 22:33pm
v2
Comments
Suvendu Shekhar Giri 3-Aug-15 4:30am    
Can you share your connection string?
Make sure that you are connecting to the correct database.
jamesmc1535 3-Aug-15 4:35am    
THanx , no need to post it fixed it, see i copied the conection string from a previous connection and forgot to change the catalog= to staff XD

Start by looking at your connection string and ensure that you are connected to the right SQL Server instance, and using the right database.
It is entirely possible that you have created the table in the development server, and using it in the production server, or vice versa.

If that's all correct, use SSMS to connect to your DB using the info in the connections string - don't assume anything, you need to be absolutely in the right place, so copy and paste all fields.
Check the DB and make sure that the table exists. It may be that you forgot to run teh query, or ran it on the wrong DB.
 
Share this answer
 
Comments
jamesmc1535 3-Aug-15 4:41am    
the problem was with my connection string
OriginalGriff 3-Aug-15 4:43am    
:thumbsup:
In addition to soultion 1 by OriginalGriff[^]...

Do not use query like this:
C#
SqlCommand cmd = new SqlCommand("INSERT INTO staff1 (name ........ect ect ect)

It might be the reason of serious problems, because such of command is SQL Injection[^] vulnerable.

How To: Protect From SQL Injection in ASP.NET[^]
SQL Injection Attacks[^]
SQL Injection and how to avoid it[^]
Do Stored Procedures Protect Against SQL Injection?[^]
 
Share this answer
 
Comments
jamesmc1535 3-Aug-15 4:52am    
thanks i did some research on the sql injection , this program im doin is just a test so sql injection aint a big threat at the moment but that@ params stuff is confusing me
Maciej Los 3-Aug-15 5:00am    
It's strongly recommended to use parameters even if it is a small, testing program.
jamesmc1535 3-Aug-15 5:31am    
yeah im doing research on it bt i have trouble understanding it,so im using this way to progress further in my program, until i fully understand the parametirized

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