7

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;
    }
Learner
  • 513
  • 2
  • 9
  • 17

7 Answers7

12

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(); 
          }
        }
     }
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
  • another thing is in that code near con.Open() & con.close() is red underlined.When i put the cursor on that then i saw Methos Stub Create.(then i saw thatone created in db connector class) Why is that ? – Learner Jun 22 '12 at 16:34
  • 1
    What exactly IS DatabaseConnector? Your own class? – Chris Gessler Jun 22 '12 at 16:38
  • 1
    @ChrisGessler - If I were to hazard a guess it was based off this code: http://www.codeproject.com/Articles/32909/Database-Connector – Security Hound Jun 22 '12 at 16:47
  • 1
    @Learner - Those lines are underlined in red because your code won't compile because those methods to do not exists for the `SqlCommand` class. You havea great deal more research before we can actually help you with additional questions. – Security Hound Jun 22 '12 at 16:52
  • 2
    @Learner - the red underline means the method doesn't exist. When you click "Implement" it puts a method stub in the class for you. I'm guessing you're not using the DatabaseConnector class properly but I don't have the source to guide you on that. – Chris Gessler Jun 22 '12 at 16:53
  • 1
    @ChrisGessler - You have an error in your code. The variable `cmd` does not exist yet you attempt to call the `ExecuteNonQuery` method. This is the reason you should suggest better variable names. – Security Hound Jun 22 '12 at 16:53
  • 1
    @Ramhound - sorry... not my code, but i'll fix it. thanks for pointing that out. – Chris Gessler Jun 22 '12 at 16:55
  • @ChrisGessler - I updated my DBconnector class in my original post – Learner Jun 22 '12 at 16:56
  • 1
    Well... I see that your DBConnector class is missing a few things. I'll try to update my answer with something usable, but it might not be easy. – Chris Gessler Jun 22 '12 at 17:02
  • @ChrisGessler i'm a beginner.no understanding about factory patterns. – Learner Jun 22 '12 at 17:34
  • @ChrisGessler - I saw another thing in one tuitorial ..they told PUT connection string in web.config... i tried that also.. LIKE THIS But i dont know how to use that in my c# file ? – Learner Jun 22 '12 at 17:37
  • @Learner - this is outside the scope of your question, but I recommend using Microsoft Enterprise Library which has a UI to add connection strings to your web.config and all the objects needed to connect and retrieve data from your SQL Server instance. – Chris Gessler Jun 22 '12 at 17:40
  • @ChrisGessler i'm sorry to asking that,but when i tried it out this SQLConnection way,but unfortunately that there was a error know.. then i tried it out in every possible way(then i found this web.config method) – Learner Jun 22 '12 at 17:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12918/discussion-between-chris-gessler-and-learner) – Chris Gessler Jun 22 '12 at 17:46
7

SqlDbType the s needs to be capitalized!

barbiepylon
  • 891
  • 7
  • 23
3
yes but theres no any case sensitivity problem know 

You actually DO have syntax errors because you used s instead of S. Furthermore SqlCommand does not have a method called Open() nor does it have one for Close()

You should be using SqlConnection since it contains the methods Open() and Close() and set the SqlCommand's Connection property to your instance of SqlConnection in order to open and close the connection to your database.

Security Hound
  • 2,577
  • 3
  • 25
  • 42
2

You have more errors.

  • Based on the way you use it, I think you mean SqlConnection instead of DatabaseConnector
  • You create an SqlCommand named com, but refer to it as cmd
  • You will need to assign the SqlConnection to the SqlCommand, or it will not know which connection to open.
  • You only provide 1 parameter to the SqlCommand, while the query needs two (ItemNo as well as ItemName).

Edit, based on your new source:

  • The error "DatabaseConnector' does not contain a definition for 'Open'" can be corrected by writing con.Connect() instead of con.Open().
  • However, the other error, that it doesn't have a "Close()" function, can't be corrected - there is no way to tell it to close a connection.

Is this your own code?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • Then add a function to it that can close the connection. Or just throw the whole DatabaseConnector away and use the SqlConnection directly. – Mr Lister Jun 22 '12 at 17:26
  • when i throughout DatabaseConnector my connectionstring also loss know ? – Learner Jun 22 '12 at 17:35
1

Are you using the class made by The Code Project ?

Well, I think the error is because the connection string. The connection string have to be in the class OdbcDatabaseConnector. Well, I never used this class of Code Project, but can be it.

MayogaX
  • 469
  • 3
  • 18
1

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

for example:

  param[0] = new SqlParameter("@Name", System.Data.SqlDbType.VarChar,50);
programmer21
  • 173
  • 11
0

Its capital 'S' in SqlDbType, after making the correction, right click on the word, mouse on over to resolve option and add the System.Data namespace. worked for me!