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();
}
In solve it by the sending the
DataTable dt
from thefirst Form cash
to thesecond Form cashagree
as a prameter by calling the method that return datagridview in cash form I wrote this:in cashagree form I wrote this
Instead of using the dtab.LoadDataRow, you should be using the dtab.Rows.Add(datarow) method. An example of how to do this:
Reference:
How to: Add Rows to a DataTable
Adding Data to a DataTable