The name "sqlDbType" does not exist in the current context

I'm going to change the value of the text box .. but I saw a problem there

protected void btn_edit_Click(object sender, EventArgs e) { DatabaseConnector con = new DatabaseConnector().CreateInstance(); SqlCommand com = new SqlCommand("UPDATE tbl_BinCardManager SET ItemName = @ItemName WHERE ItemNo = @ItemNo"); com.Parameters.Add("@ItemName",sqlDbType.VarChar); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } 

ERROR 1:

The name 'sqlDbType' does not exist in the current context

ERROR 2:

'ERPSystem.DatabaseConnector' does not contain a definition for 'Open' and no extension method 'Open' that takes the first argument of type 'ERPSystem.DatabaseConnector' can be found (do you miss using a directive or an assembly reference?)

My DBConnector class:

  class DatabaseConnector { private DatabaseConnector databaseConnector; private string connectionString = "Data Source=lernlap;Initial Catalog=ERPSystemDB;User ID=sa;Password=sa123"; public DatabaseConnector() { } private SqlConnection connection; private bool Connect() { try { connection = new SqlConnection(connectionString); connection.Open(); return true; } catch(Exception) { return false; } } internal DatabaseConnector CreateInstance() { if (databaseConnector == null) { databaseConnector = new DatabaseConnector(); databaseConnector.Connect(); } return databaseConnector; } 
+7
source share
7 answers

C # is case sensitive ... Try using intellisense.

 SqlDbType 

Other errors may disappear if you correct the first one.


On the other hand, you will encounter connection / memory leaks without proper resource handling. Personally, I use the using statement to avoid errors.

I'm not quite sure what the "DatabaseConnector" is, maybe your own class, but you should probably use SqlConnection , or maybe SqlDatabase .

Update: I'm not sure if the DBConnector class should be singleton or factory, or both, so I just simplified my answer to avoid using it. Ask another question with details on how to create the template you are looking for and provide the DBConnector class. I think it’s possible, but I just don’t have enough information to fix what you have.

 public static CONN_STR = "Data Source=lernlap;Initial Catalog=ERPSystemDB;User ID=sa;Password=sa123"; protected void btn_edit_Click(object sender, EventArgs e) { using(SqlConnection con = new SqlConnection(CONN_STR)) { con.Open(); using(SqlCommand cmd = new SqlCommand("UPDATE tbl_BinCardManager SET ItemName = @ItemName WHERE ItemNo = @ItemNo"), con) { // TODO: fill in param values with real values cmd.Parameters.AddWithValue("@ItemName", "my item name"); cmd.Parameters.AddWithValue("@ItemNo", 1); cmd.ExecuteNonQuery(); } } } 
+12
source

SqlDbType s need uppercase letters!

+7
source
 yes but theres no any case sensitivity problem know 

You really have syntax errors because you used s instead of s . Moreover, SqlCommand does not have a method called Open() and does not have it for Close()

You must use SqlConnection, as it contains the Open() and Close() methods and sets the SqlCommand Connection property to your SqlConnection instance to open and close the connection to your database.

+3
source

You have more mistakes.

  • Based on how you use it, I think you mean SqlConnection instead of DatabaseConnector
  • You create a SqlCommand named com , but call it cmd
  • You will need to assign SqlConnection to SqlCommand, otherwise it will not know which connection to open.
  • You provide only one SqlCommand parameter, and two are required for the request ( ItemNo , as well as ItemName ).

Change based on your new source:

  • The error "DatabaseConnector" does not contain a definition for "Open", can be fixed by writing con.Connect() instead of con.Open() .
  • However, another error that does not have the Close () function cannot be fixed - there is no way to tell her to close the connection.

Is this your own code?

+2
source

Are you using the class created by the Code Project ?

Well, I think the error is due to the connection string. The connection string must be in the OdbcDatabaseConnector class. Well, I never used this Code Project class, but it may be.

+1
source

If you write capital “S” but still get the same error, especially in visual studio 2015, instead of writing “SqlDbType”, write: System.Data.SqlDbType

eg:

  param[0] = new SqlParameter("@Name", System.Data.SqlDbType.VarChar,50); 
+1
source

After capitalizing S in SqlDbType, right-click on the word, hover over the solution option and add the System.Data namespace. worked for me!

0
source

All Articles