dataGridview save runtime data to sql ERROR! urgent...

  • Thread starter Thread starter Xabi AliSher
  • Start date Start date
X

Xabi AliSher

Guest
i have a datagridview tat has three four columns, and i want to save its data to sql database for the first time when i click save button it works fine and shows a messaege box that "saved" but when i click OK button of message box it prompts with an error stating INVALID OPERATION EXCEPTION WAS UNHANDLED(This SqlTransaction has completed; it is no longer usable.) AND it highlights this line of my coding ( str.Commit(); )

below is a copy of my codings....

plz help me to overcome this error.....




SqlConnection Conn = new SqlConnection();

SqlCommand cmd = new SqlCommand();

StringBuilder sb = new StringBuilder();

SqlTransaction tr;



string appConn = ("Data source=(local); Initial Catalog=PremiumPolicy; Integrated Security=True");

Conn = new SqlConnection();

if (Conn.State == ConnectionState.Open)

{

Conn.Close();


}

Conn.ConnectionString = appConn;

Conn.Open();




private void saveToolStripButton_Click(object sender, EventArgs e)

{

tr = Conn.BeginTransaction();



sb.Remove(0, sb.Length);

sb.Append("INSERT INTO RECEIVEABLE_INSTALLMENTS (REFERENCE_NO, INSTALLMENT_NO, DATE,AMOUNT)");

sb.Append("VALUES (@REFERENCE,@INSTALLMENT_NO,@DATE,@INSTALLMENTS)");

string sqlSave = sb.ToString();

for (int i = 1; i < dataGridView1.Rows.Count -1; i++)

{

cmd.CommandText = sqlSave;

cmd.CommandType = CommandType.Text;

cmd.Connection = Conn;

cmd.Transaction = tr;

cmd.Parameters.Clear();

cmd.Parameters.Add("@DATE", SqlDbType.Date).Value = dataGridView1.Rows.Cells[1].Value;

cmd.Parameters.Add("@INSTALLMENTS", SqlDbType.Decimal).Value = dataGridView1.Rows.Cells[2].Value;

cmd.Parameters.Add("@REFERENCE", SqlDbType.VarChar).Value = dataGridView1.Rows.Cells[3].Value;

cmd.Parameters.Add("@INSTALLMENT_NO", SqlDbType.Int).Value = dataGridView1.Rows.Cells[4].Value;


cmd.ExecuteNonQuery();

tr.Commit();

MessageBox.Show("Its Done!");

}

}

Continue reading...
 
Back
Top