Answered Login to DB? No. Actually, NO!!

  • Monday, August 20, 2012 2:43 AM
     
     

    C# keeps thinking that I'm trying to login to my DB...I guess.  I have no password on the DB; I'll never have a password on this if I live for 100 years.  The DB is simply for screwing around and experimenting; basically having fun.  This process, and hundreds like it, have worked fine for over 4 years.  Now, all of a sudden C# thinks the DB is password-protected, and I have no idea why.  I paired all of my code down to the very basic elements, and it still doesn't work.  If I delete any more code, all I'll have is a Form popping open, and this requires no code whatsoever.  LOL!!  I can't figure it out.  If someone could underscore what's going on, I'd really appreciate it.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            SqlConnection conn = new SqlConnection("Server=Excel-PC;Database=Northwind;Trusted_Connection=True");

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Shown(object sender, EventArgs e)
            {
                try
                {
                    conn.Open();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }
            }
        }
    }

    Finally, if C# errors made any sense at all, it would be quite simple to debug these things.  When it constantly tells you there is a problem, and it is a different problem completely, it's kind of hard to debug this stuff. 

    It's like the story about the boy who cried wolf.  We all know how that ended.


    Ryan Shuell

All Replies

  • Monday, August 20, 2012 4:55 AM
    Moderator
     
     Answered
    Has your login to your computer changed? Is the database local to your computer or on a server? Are you on a domain? Have you tried logging into the database with SSMS (SQL Server Management Studio)?

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked As Answer by ryguy72 Tuesday, August 21, 2012 9:52 PM
    •  
  • Monday, August 20, 2012 10:52 AM
     
     

    What is the error that you get?

    Also the format should be something like: "Data Source=myServerAddress;Initial Catalog=myDataBase"

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...


    • Edited by Noam B Monday, August 20, 2012 10:56 AM
    •  
  • Monday, August 20, 2012 3:24 PM
    Moderator
     
     Answered

    Noam, the database connection string that Ryan is using is just fine. That's not where his problem lies.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked As Answer by ryguy72 Tuesday, August 21, 2012 9:51 PM
    •  
  • Monday, August 20, 2012 3:41 PM
     
     Answered

    Hello,

    Just an important note about the language, the language is doing nothing unless the developer instruct it to do it.

    So saying that C# has done something doesn't make a lot of sense unless you've upgraded the framework version or upgraded the project to target a different version which might changed something to result a breaking change; otherwise, it doesn't make a lot of sense to look in your code.

    You should be checking the system, the technologies installed and more importantly the configuration rather than your own code.

    Regards,

    Eyal Shilony

    • Marked As Answer by ryguy72 Tuesday, August 21, 2012 9:51 PM
    •  
  • Monday, August 20, 2012 11:22 PM
     
     

    Thanks everyone.  It is very unstable now; it worked fine from about 5:30pm-6:30pm, and after that it stopped working altogether.  I was going to post the solution, but I literally don't have it now.  I just deleted everything.  It is just easier to restart from scratch. 

    I've found that it is usually pretty easy to rebuild a project, I just wish I didn't have to do this every time I need to debug something.  In VBA, it's easy to debug something and move on.  It seems, in Visual Studio, the project holds onto whatever is in memory, no matter what, and it won't let go.  Sometimes, the only way to fix a problem is to completely delete every file associated with the project, and start over from the beginning.  I don't feel like it's good development-strategy, but often, that's the only thing that works.  I'd rather rebuild a project in 1/2 hour, then spend 12 hours debugging it, make no progress whatsoever, and then rebuild the whole thing in 1/2 hour.

    I'll post back with a working solution later tonight, or tomorrow.

    It actually doesn't have anything at all to do with a password, or anything close to that.  The hardest part about working with Visual Studio, for me, is that it always gives false errors.  You can easily end up spending hours trying to debug something that is completely irrelevant to the actual problem.  Occasionally, the debugger is right, but that's pretty rare.


    Ryan Shuell

  • Tuesday, August 21, 2012 9:51 PM
     
     

    Basically, what I was trying to do was list multiple items from a SQL Server Table to both a ComboBox and a ListBox.  Here is the working code:

            private void Form1_Load(object sender, EventArgs e)
            {
                //Data Source=(local);Initial Catalog=NORTHWIND.MDF;Integrated Security=True
                SqlConnection cs = new SqlConnection("Data Source=EXCEL-PC;Initial Catalog=NORTHWIND.MDF;Integrated Security=True");
                SqlDataAdapter da = new SqlDataAdapter("Select Distinct CompanyName From Customers Order By CompanyName", cs);

                DataTable dt = new DataTable();
                da.Fill(dt);


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    comboBox1.Items.Add(dt.Rows[i]["CompanyName"]);
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listBox1.Items.Add(dt.Rows[i]["CompanyName"]);
                }

            }

    This was just a learning exercise for me; trying to #en my C# skills.  Not sure what was going on yesterday, but the debugger wasn't helping me one bit!!  I think this is a case of me needing to spend significantly more reading C# books, and similarly, I need to spend much more time in a C# environment, learning by doing.

    Thanks for trying to help, everyone.


    Ryan Shuell

  • Tuesday, August 21, 2012 10:26 PM
    Moderator
     
     Answered

    Hi Ryan,

    Here is the working code:

    I assume that means that you got it working ok?

     It is very unstable now; it worked fine from about 5:30pm-6:30pm, and after that it stopped working altogether

    It seems, in Visual Studio, the project holds onto whatever is in memory, no matter what, and it won't let go.  Sometimes, the only way to fix a problem is to completely delete every file associated with the project, and start over from the beginning.  I don't feel like it's good development-strategy, but often, that's the only thing that works. 

    There's really nothing wrong with Visual Studio ... I'd say that there's something wrong with your environment. If everyone had to delete every file and project and start over again every time they had a problem, then Visual Studio would not be as widely used as it is today. I have been using Visual Studio for 10 years and have NEVER had to resort to deleting everything and starting over from scratch.

    Of  course, the question is: what's wrong with your environment? That's another matter altogether ... and sometimes it's hard to figure out.

    The code you initially posted looked fine. The last bit of code you posted looked fine too. If there's something screwy with your environment, you'll probably start having problems again. FWIW, anytime my machine starts acting wonky (whether it's Visual Studio or just some other app running), and I can't figure out what's wrong and nothing makes any sense,  I always assume it's time for a reboot. 


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked As Answer by ryguy72 Tuesday, August 21, 2012 10:58 PM
    •  
  • Tuesday, August 21, 2012 10:58 PM
     
     

    Thanks for the follow up, Bonnie!  Yes, everything works fine now.  Rebooting is great advice.  I tell others, both in my office and in these discussion forums, to do the very same, when thinks get flukey and nothing really makes sense.  I guess I forgot to follow my own advice.  Also, obviously, learning C# better will help quite a bit!!

    Thanks again!!


    Ryan Shuell