Hello xD

First : Im new to c# stuff

Im having some windows form app. I read data from datagridview(iv puted in from table..works fine) and i should write it to table in database..
My insert statment doesnt do nothing..

  private void button3_Click(object sender, EventArgs e)
        {

            int amorplanid = 0;
            int idn = 0;
            DateTime datum;
            double interest = 0;
            double principal = 0;
            double payment = 0;
            double newprincipal = 0;

            string nizz = "";
            string[] niz= new string[7];
                for (int x = 0; x < dataGridView1.Rows.Count-1; x++)
                {
                    for (int j = 0; j < dataGridView1.Rows[x].Cells.Count; j++)
                    {

                        nizz += dataGridView1.Rows[x].Cells[j].Value.ToString()+"."; 
                    }
                    niz = nizz.Split('.');

                    amorplanid = System.Convert.ToInt32(niz[0]);
                    idn = System.Convert.ToInt32(niz[1]);
                   // datum = System.Convert.ToDateTime(niz[2]);
                    datum = DateTime.Now;
                    interest = System.Convert.ToDouble(niz[3]);
                    principal = System.Convert.ToDouble(niz[4]);
                    payment = System.Convert.ToDouble(niz[5]);
                    newprincipal = System.Convert.ToDouble(niz[6]);

                   String insert = @"INSERT INTO AmortPlanCoupT(ID, AmortPlanID, CoupDate, Interest, Principal, Payxment, NewPrincipal) VALUES (" + idn + "," + amorplanid + "," + datum + "," + (float)interest + "," + (float)principal + "," + (float)payment + "," + (float)newprincipal + ")";
                      SqlConnection myconn = new SqlConnection(conn);
                     // String MyString = @"INSERT INTO Employee(ID, FirstName, LastName) VALUES(2, 'G', 'M')";
                      try
                      {
                          myconn.Open();
                          SqlCommand cmd = new SqlCommand(insert, myconn);
                          cmd.ExecuteNonQuery();
                          myconn.Close();
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show(ex.ToString());
                      }
                }

                label14.Text = niz[0];

        }

So iv created simple console app to add 2 values to table and it doest work eather :\

 SqlConnection MyConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\Database1.mdf;Integrated Security=True;User Instance=True");

                      try
                      {
                         MyConnection.Open();

                            String MyString = @"INSERT INTO test(id, leto) VALUES(2, 2)";
                            SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);

                            MyCmd.ExecuteNonQuery();
                            MyConnection.Close();

                      }
                      catch (Exception e)
                      {
                          Console.WriteLine(e.ToString());
                      } 
        }

Mybe for help :
Iv got hint on other forum that conn string should not include AttachdbFile...string should be like this :
Data Source=.\SQLEXPRESS;Database=Database1;Integrated Security=True
Just like i said im beginner .. :D

tnx

Recommended Answers

All 3 Replies

What does the Exception say? The error message?
But based on your description, there is no error, so it means that the data are inserted into datatabe - but you cannot see the insertions, since this database is attached to your project.
Try to write the full path to the database, instead of using |DataDirectory|.

Mitja hello and tnx for ur reply

iv changed conn string :

string connectionString=@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\f1shst1ck\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Beni.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

ill try to change it in app setts just like you suggested on other topic.
Iv changed INTO (id , leto ->changed to x ) and i get an error.. so i think connection iis ok ?

Are you running your code in debug mode?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.