C# OleDb dataGridView fill with textbox

-1

(Sorry for my bad english) I made this program for my dad, so please help me.

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        connetionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Csaba\\Desktop\\Teszt.accdb ;";
        connection = new OleDbConnection(connetionString);
        Sql = "select * from 2017";
        try
        {
            connection.Open();
            oledbAdapter = new OleDbDataAdapter(Sql, connection);
            oledbAdapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

    private void button3_Click(object sender, EventArgs e)
    {
        string[] rowS = { NumeleO, AdresaO, NrMatricolO, VJO, NrMO, DataO, TaurO, CodS, };


   int row = 0;
        row = dataGridView1.Rows.Count - 1;
        dataGridView1.Rows.Add(new Object[] { NumeleTxT.Text, AdresaTxT, NrMatricolTxT, VJTxT, NrMTxT, DataTxT, TaurTxt, CodTxT });
        dataGridView1.Refresh();
    }
}

Error: System.InvalidOperationException occurred

  HResult=0x80131509
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.DataGridViewRowCollection.Add(Object[] values)
   at Apu_Proba_3.Form1.button3_Click(Object sender, EventArgs e) in C:\Users\Csaba\documents\visual studio 2017\Projects\Apu Proba 3\Apu Proba 3\Form1.cs:line 79
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
 at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
 at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
 at Apu_Proba_3.Program.Main() in C:\Users\Csaba\documents\visual studio 2017\Projects\Apu Proba 3\Apu Proba 3\Program.cs:line 19

I don't know what's the problem. Please help. I will answer all your questions.

c#
datagridview
oledb
asked on Stack Overflow Mar 2, 2017 by user7650273 • edited Mar 3, 2017 by 0ndre_

1 Answer

0

For InvalidOperationExceptions, it is best to always check the documentation of the method (System.Windows.Forms.DataGridViewRowCollection.Add(Object[] values)) throwing the exception to see what cases the exception is thrown.

Since you are adding values manually, I would first check to see whether you are violating this case first:

The row returned by the DataGridView.RowTemplate property has more cells than there are columns in the control.

And the check whether you're violating this case:

The DataGridView has no columns.

If it is neither of these cases, go through the remaining exception cases to see if you are violating them.

answered on Stack Overflow Mar 3, 2017 by Adrian

User contributions licensed under CC BY-SA 3.0