Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

c#-4.0

I'm going to edit textbox value.. but i saw there's a problem

  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' accepting a first argument of type 'ERPSystem.DatabaseConnector' could be found (are you missing a using directive or an assembly reference?)

My DBConnector Class is :

 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;
    }
like image 431
Learner Avatar asked Jun 22 '12 16:06

Learner


2 Answers

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

SqlDbType

The other errors may disappear if you correct the first one.


On a side note, you're going to run into connection/memory leaks without proper resource handling. Personally, I use the using statement to avoid the pitfalls.

I'm not entirely certain what "DatabaseConnector" is, possible your own class, but you should probably be using SqlConnection instead, or possibly SqlDatabase.

Update: I'm not sure if the DBConnector class is supposed to be a singleton or a factory, or both - so I just simplified my answer to avoid using it. Ask another question with detail on how to create the pattern you're looking for and provide the DBConnector class. I think it's do-able, but I just don't have enough info 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(); 
          }
        }
     }
like image 92
Chris Gessler Avatar answered Nov 28 '22 21:11

Chris Gessler


SqlDbType the s needs to be capitalized!

like image 23
javajavajava Avatar answered Nov 28 '22 22:11

javajavajava