简体   繁体   中英

losing data entered in dataview C#

I am using Visual C# 2008 to make a application that takes the data from textboxes and displays it in datagridview in another form the conforming make it entered to the database. I send the data using dataTable with a function entered the data without any symentic error but when I call the other for the datagridview comes empty and the database comes empty. When I duplicate a primary key it gives an error stating "cannot duplicate primary key" .

This is the code for the function that transfers that datatable

public DataTable showout() {
    DataTable dtab = new DataTable();
    DataColumn dc1 = new DataColumn("رقم المتسلسل");
    DataColumn dc2 = new DataColumn("رقم الحساب");
    DataColumn dc3 = new DataColumn("أسم الحساب");

    dtab.Columns.Add(dc1);
    dtab.Columns.Add(dc2);
    dtab.Columns.Add(dc3);

   // Create an array for the values. 
   object[] newRow = new object[3];

    // Set the values of the array.
     string s = numb.Text;


     newRow[0] =numb.Text;
     newRow[1] = textBox5.Text;
     newRow[2] =note.Text;


     DataRow row;

     dtab.BeginLoadData();


     // Add the new row to the rows collection.
     row = dtab.LoadDataRow(newRow, true);

    return dtab;
}

this is the code that I call the function in the other From

private void Cashagree_Load(object sender, EventArgs e) {
  dataGridView1.DataSource = ch.showout();
}

the second datagrid entering function its in the same class

    private void button1_Click(object sender, EventArgs e)
    {

        dataGridView1.Visible = true;
       dataGridView1.DataSource = showout();

        entering(true);
    }

and this is the entering to the database

   public void entering(bool bl)
      {try{
        if (bl)
        {

            using (SqlConnection connection = new SqlConnection(connectionString))
            {

                DateTime Date = DateTime.Today;


                SqlCommand cmd = new SqlCommand("INSERT INTO Accont(Account_ID,Account_Name,Owners,Curency,Curncytype,Depet,Credet_devet,Date,Note) VALUES (@AccountID, @AccountName, @Owner, @Curncy,@Curncytype,@Depet,@Cridetdevet,@Date,@Note)");
                cmd.CommandType = CommandType.Text;
                cmd.Connection = connection;
                cmd.Parameters.AddWithValue("@AccountID",numb.Text);
                cmd.Parameters.AddWithValue("@AccountName", comboBox1.SelectedText.ToString());
                cmd.Parameters.AddWithValue("@Owner", owner.Text);
                cmd.Parameters.AddWithValue("@Curncy", curency.Text);
                cmd.Parameters.AddWithValue("@Curncytype", curncyval.Text);
                cmd.Parameters.AddWithValue("@Depet", Depet.Text);
                cmd.Parameters.AddWithValue("@Cridetdevet", textBox5.Text);
                cmd.Parameters.AddWithValue("@Date", Date);
                cmd.Parameters.AddWithValue("@Note", note.Text);
                connection.Open();//Owner
                cmd.ExecuteNonQuery();}}
    }
        catch(Exception ee)
        {MessageBox.Show(ee.Message);}

the conforming from the another form

   private void button1_Click_1(object sender, EventArgs e)
    {
            ch.entering(true);
            Close();

    }

Instead of using the dtab.LoadDataRow, you should be using the dtab.Rows.Add(datarow) method. An example of how to do this:

public DataTable showout()
{
  DataTable dtab = new DataTable();
  // More efficient way of adding the columns with types:
  dtab.Columns.Add("رقم المتسلسل", typeof(String));
  dtab.Columns.Add("رقم الحساب", typeof(String));
  dtab.Columns.Add("أسم الحساب", typeof(String));
  /*
  DataColumn dc1 = new DataColumn("رقم المتسلسل");
  DataColumn dc2 = new DataColumn("رقم الحساب");
  DataColumn dc3 = new DataColumn("أسم الحساب");

  dtab.Columns.Add(dc1);
  dtab.Columns.Add(dc2);
  dtab.Columns.Add(dc3);
  */

  // Create a new row using the .NewRow method
  DataRow datRow = dtab.NewRow();
  datRow["رقم المتسلسل"] = numb.Text;
  datRow["رقم الحساب"] = textBox5.Text;
  datRow["أسم الحساب"] = note.Text;

  // Add the new row to the DataTable
  dtab.Rows.Add(datRow);

 return dtab;
}

Reference:

How to: Add Rows to a DataTable
Adding Data to a DataTable

In solve it by the sending the DataTable dt from the first Form cash to the second Form cashagree as a prameter by calling the method that return datagridview in cash form I wrote this:

  cashagree gc2 = cashagree(showout()); 

in cashagree form I wrote this

  DataTable dt = new DstsTsble();
 public cashagree(DataTable d2){
   dt =d2;

 }
 and in the load of `cashagree_Load` I asign the datagridview datasoure 

   private void Cashagree_Load(object sender, EventArgs e)
    {
        if (dt.Rows[0].IsNull(dt.Columns[0]))
        {
            MessageBox.Show("There no primary key");
            Close();
        }
        dataGridView1.DataSource = dt;
        dataGridView1.Columns[7].Visible = false;// Iwantn't all the datatable so I diapple some Columns
        dataGridView1.Columns[5].Visible = false;
        dataGridView1.Columns[7].Visible = false;
        dataGridView1.Columns[6].Visible = false;
        dataGridView1.Columns[4].Visible = false;



      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM