Devin

Devin

  • NA
  • 77
  • 0

Adding a Row to a DataTable from a function

Jun 19 2009 10:52 AM
Ok so im really new to C# and just need to know how to properly call this function. I have a table dtPad.
       
 public void save_sheet(ref DataTable dtPad) ///Adds the rows to the table///
        {
            DataRow nrPad;
            nrPad = dtPad.NewRow();
            nrPad["Title"] = textBox1.Text;
            nrPad["Short Description"] = textBox2.Text;
            nrPad["Long Description"] = textBox3.Text;
            nrPad["Category"] = comboBox1.Text;
            nrPad["Date Set"] = dateTimePicker1.Value;
            nrPad["Date Saved"] = System.DateTime.Now;
            nrPad["Text"] = textBox4.Text;
            dtPad.Rows.Add(nrPad);
        }
And a function (above) that will add the data to the table when called.
So when a user clicks a button this code runs.
         private void saveToolStripMenuItem_Click(object sender, EventArgs e) ///Save Sheet Menu Item///
        {
            {
                if (check_data() == (true))
                {
                    if (MessageBox.Show("Do you want to save?", "Notepad", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        save_sheet(ref dtPad);
                    }
                }
                else
                {
                }
            }
However im having a hard time calling the save_sheet function while im passing the table to the function. Im getting this error:
Error    1    The name 'dtPad' does not exist in the current context    
Error 2 The best overloaded method match for 'Simple_Notepad.Form1.save_sheet(ref System.Data.DataTable)' has some invalid arguments
Error 3 Argument '1': cannot convert from 'ref dtPad' to 'ref System.Data.DataTable'

Any light you can shed on this topic would be wonderful because after 2 hours of Googleing i just don't know how to fix this.

Answers (2)