Could not open Connection to SQL Server.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mikkeee
    New Member
    • Feb 2013
    • 94

    #46
    This tells me that it's only dropping into your 'if' condition 1 time. Put a breakpoint in your foreach to see why.
    Code:
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            // Put breakpoint on line below
            Console.WriteLine("");
            if (!row.IsNewRow)
            {
                command.Parameters["@Date"].Value = row.Cells[0].Value.ToString();
                command.Parameters["@LogName"].Value = row.Cells[1].Value.ToString();
                command.Parameters["@Channel"].Value = row.Cells[2].Value.ToString();
                command.Parameters["@DateRecord"].Value = row.Cells[3].Value.ToString();
                // Make sure "row.Cells[4].Value" returns a float or convert it before setting the parameter value
                command.Parameters["@SizeInBytes"].Value = row.Cells[4].Value;
                command.ExecuteNonQuery();
            }
        }

    Comment

    • M1kkelZU
      New Member
      • Feb 2013
      • 80

      #47
      Ok I know what I did wrong, When editing the code I left command.Execute NonQuery(); out side of my foreach loop, I added this into the 'if' condidtion and now its adding everything, except 1 thing which is SizInBytes, it only places the value 1. Maybe because I can't use the DbType.Float statement. At the moment I have DbType.String on all of them as the DbType doesn't use the Values I've declared in my database, Maybe I need to use int64 or something with the DbType part, not sure though.

      I had to change:
      Code:
      command.Parameters.Add("@SizeInBytes", DbType.String);
      to

      Code:
      command.Parameters.Add("@SizeInBytes", DbType.Int64);
      and in SQLite Admin the Database property from
      Float

      to

      NUMERIC.

      So All I have to do now is hope that it doesn't break when I have to showcase this thing lol.

      Thanks alot Mikkeee, I know it must've been annoying with my noob way of coding but it was a nice introduction to SQL/SQLite and Database management of a program.

      Comment

      • VanessaMeacham
        New Member
        • Sep 2012
        • 31

        #48
        HI ! I didn't get your connection string and it's field plz be specify it.

        Comment

        • M1kkelZU
          New Member
          • Feb 2013
          • 80

          #49
          Connection String
          Code:
          string conString = "Data Source=<PATH TO DATABASE>"
          to use the string then just use this:
          Code:
          SQLiteConnection conn = new SQLiteConnection(constring)
          but you can go the long way around and use this:

          Code:
          SQLiteConnection conn = new SQLiteConnection("Data Source=<PATH TO DATABASE>");

          Comment

          • Mikkeee
            New Member
            • Feb 2013
            • 94

            #50
            No problem M1kkelZU... we were all noobs at one point. Glad you stuck with it and got it figured out!

            Comment

            • M1kkelZU
              New Member
              • Feb 2013
              • 80

              #51
              Yeah and now I can pass this wisdom on to my noobier friends and be seen as a God to them lol. I really appreciate all your help man.

              Comment

              Working...