50622

SQL query in C#, Invalid object name 'userInfo'

Question:

SqlConnection scon = new SqlConnection(arthurDBConnectionString); SqlDataAdapter da = new SqlDataAdapter(); da.InsertCommand = new SqlCommand("INSERT INTO userInfo VALUES(@firstname, @lastname)", scon); da.InsertCommand.Parameters.Add("@firstname", SqlDbType.VarChar).Value = firstname; da.InsertCommand.Parameters.Add("@lastname", SqlDbType.VarChar).Value = lastname; scon.Open(); da.InsertCommand.ExecuteNonQuery(); <--Exception(Invalid object name) scon.Close();

First off I would like to say that I am new to SQL. I am working on an application in C# that will manipulate data on a SQL Server. Above is the code I have for adding data to two columns of a table but I get the error that 'userInfo' (the table) is an invalid object name. I have made sure that this table exists in the database and that the connection is Open. I have also tried referring to the table as DATABASENAME.SCHEMA.userInfo and SCHEMA.userInfo with the same results. Thanks in advance for any help or input.

Answer1:

If you are getting that error then there is NOT a 'userInfo' table in your scope. Try logging in with management studio and using query analyzer to run the same command. Make sure you use the same login information in arthurDBConnectionString, debug your method and hover over it to make sure you are using the same information that is causing the error.

1) If your collation is case sensitive (check with SELECT SERVERPROPERTY('COLLATION')), the capitalization of 'userInfo' must be identical.

2) Are you in the right database? Maybe your connect string mis-spells the property (i.e. INITIL CATALOG=dbname, try another command in your code SELECT db_name() to make sure.

3) Is the schema the same?

4) Do you have permission to select from the table?

Answer2:

Check, 1)Check the table you have created exist in same data base that you provided in connection string. 2) Cross check table name

Answer3:

Instead of this

da.InsertCommand.Parameters.Add("@firstname", SqlDbType.VarChar).Value = firstname; da.InsertCommand.Parameters.Add("@lastname", SqlDbType.VarChar).Value = lastname;

use this

da.InsertCommand.Parameters.Add("@firstname", SqlDbType.VarChar).Value = textBox1.Text; da.InsertCommand.Parameters.Add("@lastname", SqlDbType.VarChar).Value = textBox2.Text

Recommend

  • How to write Delphi ActiveX control which can be used in MS Word/Excel/Powerpoint?
  • Can I customize a Jackson ObjectMapper by adding a module?
  • How can Delete be both a DDL and a DML statement
  • Primefaces :radioButton inside a ui:repeat
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • pyodbc doesn't report sql server error
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • How do I superscript characters in a UIButton?
  • Inline R code in YAML for rmarkdown doesn't run
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • PHP - How to update data to MySQL when click a radio button
  • Optimizing database types to compact database (SQLite)
  • Counter field in MS Access, how to generate?
  • Resize panoramic image to fixed size
  • TFS: Get latest causes slow project reloading
  • Volusion's generic SQL folder, functionality
  • Running a C# exe file
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • Matrix multiplication with MKL
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • Trying to get generic when generic is not available
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • embed rChart in Markdown
  • File not found error Google Drive API
  • How can I remove ASP.NET Designer.cs files?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • java string with new operator and a literal