Page 1 of 2 12 LastLast
Results 1 to 40 of 58

Thread: Database connection issue

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Database connection issue

    can someone tell me ???. Why the following code is not working ???. let me know some idea.
    Code:
     private bool OpenConnection(ref OleDbConnection conn)
            {
                try
                    {
                    conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
                    conn.Open();
    
                    if (conn.State!=conn.Open )
                         {
                        MessageBox.Show("Connection is Not Open");
                        return false;
                       
                         }
                    else
                        {
    
                        return true;
                        
                         }
    
    
                    
                       }
                catch (Exception ex)
                       {
                    MessageBox.Show(ex.Message);
                     return false;
                       }
            }
    Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Database connection issue

    vb Code:
    1. if (conn.State!=conn.Open )

    Should be:


    vb Code:
    1. if (conn.State!=ConnectionState.Open )
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Database connection issue

    That should be

    Code:
     if (con.State != ConnectionState.Open)
                {
    
                }
    Please mark you thread resolved using the Thread Tools as shown

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    Still not working .it says The name 'ConnectionState' does not exist in the current context.here is the following code.
    Code:
    private bool OpenConnection(ref OleDbConnection conn)
             {
                               conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
                    conn.Open();
    
                     if (conn.State!=ConnectionState.Open ) 
                         {
                        MessageBox.Show("Connection is Not Open");
                        return false;
                       
                         }
                    else
                        {
    
                        return true;
                        
                         }
    
    
                    
                       }
               
                       }
              }
    Last edited by firoz.raj; Jul 21st, 2010 at 06:31 AM.

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Database connection issue

    You are missing namespace System.Data

    Code:
    if (con.State != System.Data.ConnectionState.Open )
    Please mark you thread resolved using the Thread Tools as shown

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    Resolved
    Last edited by firoz.raj; Jul 21st, 2010 at 06:31 AM.

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Database connection issue

    Quote Originally Posted by firoz.raj View Post
    already using System.Data.OleDb ; is included at the very bigining of code window.

    additional why the following code is not working ???
    Code:
    OleDbConnection conn=new OleDbConnection();
    Not working means ?
    Please mark you thread resolved using the Thread Tools as shown

  8. #8
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [RESOLVED] Database connection issue

    here is a better and more elegant code:

    Code:
    private bool OpenConnection(ref OleDbConnection conn)
    {
    	conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
    	conn.Open();
    	return conn.State == ConnectionState.Open; 
    }
    shorter lines of code and easy to read.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: [RESOLVED] Database connection issue

    here is a better and more elegant code:
    why the following code is not working ???.i simple want to fill my dataset.after filliling the listbox.let me know some idea.
    Code:
    private void BtFillListBox_Click(object sender, EventArgs e)
              {
                if (!OpenConnection())
                {
                    MessageBox.Show("Connection is Not Open");
                    this.Dispose(true);
    
                }
                   OleDbCommand cmd = new OleDbCommand();
                   cmd.CommandText = "Select * from employees";
                   DataSet ds = new DataSet("Employees");
                   OleDbDataAdapter MyAdapter = new OleDbDataAdapter();
                //   MyAdapter.SelectCommand = OleDbCommand;
                   MyAdapter.Fill(ds, "Employees");
                   
                      
                           
    
                   }
    Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.

  10. #10
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [RESOLVED] Database connection issue

    when you say it doesnt work, what happens?
    you commented out the MyAdapter.SelectCommand. this is incorrect, the DataAdapter needs this to execute the select command to fill the dataset. this select command is stored in the object you created called cmd.

    MyAdapter.SelectCommand = cmd;

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: [RESOLVED] Database connection issue

    Still i am getting error .Select Command.Connection Property has not been initialized.here is the following code.what i have written.let me know please .
    Code:
    using System;
    using System.Data.OleDb ;
    using System.Windows.Forms;
    using System.Data;
    
    namespace MakeConnection
    {
        public partial class Form1 : Form
           
         {
           
        private  OleDbConnection conn = new OleDbConnection();
            
            
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                if (!OpenConnection()) 
                    {
                   MessageBox.Show("Connection is Not Open");
                   this.Dispose(true);
              
                      }
                       filllistbox();
                 
                    }
    
    
    
    
    
       public  bool OpenConnection()
             {
                try
                    {
                // OleDbConnection conn = new OleDbConnection();
                    conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_table.mdb");
                    conn.Open();
    
                     if (conn.State!=ConnectionState.Open ) 
                            {
                        MessageBox.Show("Connection is Not Open");
                        return false;
                            }
                      else
                            {
                        return conn.State == ConnectionState.Open;
                            }
                    
                       }
                catch (Exception ex)
                      {
                    MessageBox.Show(ex.Message);
                    return false;
                      }
               }
    
           
    
    
           
            private void CloseConnection()
               {
                try
                 {
    
                if (conn.State  == ConnectionState.Open )
                        {
                    conn.Close() ;
                         }
                    conn.Dispose(); 
                    
                   }
                 catch(Exception ex)
                    {
                  MessageBox.Show(ex.Message);
                  conn.Close(); 
                   
                    }
                  }
    
    
    
    
    
    
    
            private void filllistbox()
             {
                if (!OpenConnection())
                       {
                    MessageBox.Show("Connection is Not Open");
                    this.Dispose(true);
                        }
    
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandText = "Select * from employees";
                
                
                OleDbDataAdapter dataloader = new OleDbDataAdapter();
                 dataloader.SelectCommand = cmd;
    
                  DataSet ds = new DataSet("Employees"); 
                  dataloader.Fill(ds, "Employees");
    
            }
    
    
    
    
    
    
    
    
    
    
    
          }
     }
    Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.

  12. #12
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    you should read the MSDN Documentation on how to use the DataAdapter classes.

    you need to assign, as it says, the connection property of the select command.

    OleDbCommand cmd = new OleDbCommand();
    cmd.CommandText = "Select * from employees";

    cmd.Connection = this.conn;

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    you should read the MSDN Documentation on how to use the DataAdapter classes.

    you need to assign, as it says, the connection property of the select command.
    still i am getting error .when i tried to run.additional what is the uses of this.conn.here is it this Pointer ???.when i run using f8. i got the the following error.
    The SelectCommand property has not been initialized before calling 'Fill'.
    Code:
    private void filllistbox()
             {
                if (!OpenConnection())
                       {
                    MessageBox.Show("Connection is Not Open");
                    this.Dispose(true);
                        }
    
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandText = "Select * from employees";
                cmd.Connection =this.conn ;
                
                OleDbDataAdapter dataloader = new OleDbDataAdapter();
                // dataloader.SelectCommand = cmd;
    
                  DataSet ds = new DataSet("Employees"); 
                  dataloader.Fill(ds, "Employees");
    
            }

  14. #14
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    I had stated the error previously and corrected the code

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    how should i fill listbox ???.let me know please.any help would be highly appreciated.
    Code:
     private void filllistbox()
             {
                if (!OpenConnection())
                       {
                    MessageBox.Show("Connection is Not Open");
                    this.Dispose(true);
                        }
    
                 OleDbCommand cmd = new OleDbCommand();
                 cmd.CommandText = "Select * from employees";
                 cmd.Connection =this.conn ;
                
                 OleDbDataAdapter dataloader = new OleDbDataAdapter();
                // dataloader.SelectCommand = cmd;
    
                  DataSet ds = new DataSet("Employees"); 
                  dataloader.Fill(ds, "Employees");
                  ListBox lbox = new ListBox();
                    
    
            }

  16. #16
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    not sure why you are creating a listbox control in code unless you are going to be adding it to the form dynamically?

    simply set the datasource, datamember properties on the listbox in question to the dataset/datatable

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    simply set the datasource, datamember properties on the listbox in question to the dataset/datatable
    still i am getting error at this line For Each row As DataRow In ds.Tables(0).Rows.let me know someone please.here is the following code.what i have written.
    Code:
    private void filllistbox()
                   {
                if (!OpenConnection())
                        {
                    MessageBox.Show("Connection is Not Open");
                    this.Dispose(true);
                         }
    
                 OleDbCommand cmd = new OleDbCommand();
                 cmd.CommandType = CommandType.Text;
                 cmd.CommandText = "Select * from employees";
                 cmd.Connection =this.conn ;
                
                 OleDbDataAdapter dataloader = new OleDbDataAdapter();
                 dataloader.SelectCommand = cmd;
    
                  DataSet ds = new DataSet("Employees"); 
                  dataloader.Fill(ds, "Employees");
                   
                  For Each row As DataRow In ds.Tables(0).Rows
                       { 
                   ListBox1.Items.Add(row("Name").ToString);
                       }
    
                  
    
                            
                       
                      }

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Database connection issue

    Don't just tell us that there's an error. If there's an error then there's an error message. The IDE gives it to you to help you solve the problem. Logic dictates that, if you want us to help you solve the problem, you give it to us. You've already been prompted to do that at least twice in this thread alone.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    the code you have highlighted you are using is VB.NET. but you are using C#. these are 2 different languages. you need to convert your code to C# equivelent.

    foreach(DataRow currentRow in ds.Tables[0].Rows)
    {
    ...
    }

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    still i am getting one error .'System.Data.DataRow' is a 'type' but is used like a 'variable'.datarow is a type means let me clarify please.
    Last edited by firoz.raj; Aug 22nd, 2010 at 08:30 AM.

  21. #21
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    try to read the error message and understand what it means. your syntax is also incorrect. Are you using Visual Studio IDE? if so, then there is no excuse or any reason why your syntax should be incorrect

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  22. #22
    Lively Member
    Join Date
    Jan 2008
    Location
    Belfast - N.Ireland
    Posts
    97

    Re: Database connection issue

    listBox1.Items.Add(DataRow("Name").tostring);

    should be

    listBox1.Items.Add(currentRow["Name"].tostring);

  23. #23
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    incorrect

    take a look at your syntax.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  24. #24

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    Are you using Visual Studio IDE? if so, then there is no excuse or any reason why your syntax should be incorrect
    how many visual IDE Is Available?.additional when i tried to run the following code .i am still getting error. 'currentRow' is a 'variable' but is used like a 'method'
    Last edited by firoz.raj; Aug 22nd, 2010 at 08:31 AM.

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Database connection issue

    First up, in post #22 you were told that you need to change DataRow("Name") to currentRow["Name"] and you've instead changed it to currentRow("Name"). If you aren't going to follow the advice that people provide then there's not much point asking for it.

    You've also been told MANY times that C# is a case-sensitive language and you still choose to ignore that. There is no method named "tostring". There is, however, a method named "ToString". Finally, while VB allows you to omit the parentheses on methods if the argument list is empty, C# requires ALL method calls to include parentheses.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  26. #26
    Lively Member
    Join Date
    Jan 2008
    Location
    Belfast - N.Ireland
    Posts
    97

    Re: Database connection issue

    Quote Originally Posted by Techno View Post
    incorrect

    take a look at your syntax.
    i was hoping he'd be able to guess the tostring bit himself lol

  27. #27

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    yes now the code is working fine.additional can you tell me what is the uses of this keyword here??? .is it this pointer like c++??.let me clarify please.
    Code:
    cmd.Connection =this.conn ;

  28. #28
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  29. #29
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Database connection issue

    VB -> Me == C# ->this


  30. #30

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    .as we know executeNonQuery Function returns No of Rows Affected.in the following code i want to see.how much record has beed deleted.but i am getting error.cannot convert from 'method group ' to 'string'.let me know please.
    Code:
    private void Del()
                        {
                            Int64   d;
                     OleDbCommand cmd=new OleDbCommand();
                       cmd.Connection =this.conn;
                       cmd.CommandText ="DELETE CDs.ArtistName FROM CDs";
                 :)
                    MessageBox.Show(d.ToString );
                          }
    
            private void Delete_Click(object sender, EventArgs e)
            {
                Del();
            }

  31. #31
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Database connection issue

    As I have already stated, C# requires you to include parentheses on every method call, whether you're passing parameters or not. ToString is a method. You can't call it without parentheses.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  32. #32
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Database connection issue

    It doesn't matter anyways, since the command doesn't get executed... the code as it stands will simply display "0" in the messagebox.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  33. #33

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    It doesn't matter anyways, since the command doesn't get executed... the code as it stands will simply display "0" in the messagebox.
    it is working nice .what is a issue in the above code ???.Additional i have tried to insert textbox data into the listbox.but getting error at red line.let me know please.[Code]private void BtAdd_Click(object sender, EventArgs e)
    Last edited by firoz.raj; Aug 22nd, 2010 at 08:30 AM.

  34. #34
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    CommandText requires a string.

    you arent providing a string.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  35. #35

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    Still i am getting expected error ;.at the following line.Kindly let me know the idea
    Last edited by firoz.raj; Aug 22nd, 2010 at 08:30 AM.

  36. #36
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Database connection issue

    Quote Originally Posted by firoz.raj View Post
    Still i am getting expected error ;.at the following line.Kindly let me know the idea.
    Code:
    cmd.CommandText ="insert into CDs(ArtistName )values ('"txtArtistName"')";
    Code:
    cmd.CommandText ="insert into CDs(ArtistName) Values ('"txtArtistName.Text"')";

  37. #37

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    still i am getting ; Expected Error.at the following line .
    Code:
    cmd.CommandText ="insert into CDs(ArtistName )values ('"txtArtistName.text"')";

  38. #38
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    because you need to add the value of txtArtistName.Text into the string.

    cmd.CommandText ="insert into CDs(ArtistName )values ('" + txtArtistName.text + "')";

    for best practice you should be using StringBuilder to concatinate string as well as using SPROCS (Stored Procedures) for data insertion, deletion and retrieval for security and performance

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  39. #39

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Database connection issue

    Well.Can SomeOne Tell me ?How should i refresh the list Box.after adding data
    it should immediately go in the listbox.let me know please.
    Code:
     private void BtAdd_Click(object sender, EventArgs e)
                   {
               OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = this._conn;
                cmd.CommandText ="insert into CDs(ArtistName )values ('" + txtArtistName.Text + "')";
                cmd.ExecuteNonQuery();
                //filllistbox();
                    }
    Last edited by firoz.raj; Aug 10th, 2010 at 12:43 AM.

  40. #40
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Database connection issue

    you need to rebind the data. so you need a method which binds the data and call that method.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width