Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
158 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...