deleting a selected row from datagridview it wont delete?

Posted by Sudheep.grandhe under C# on 12/19/2011 | Points: 10 | Views : 3993 | Status : [Member] | Replies : 5
Hi,

deleting a selected row from datagridview it wont delete?
private void btn_delete_Click_1(object sender, EventArgs e)
{
SqlTransaction tran = null;
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd;
int invoiceno = 1;
try
{
if (con.State == ConnectionState.Closed)
con.Open();
tran = con.BeginTransaction();
cmd = new SqlCommand("delete from tbl_report1 where invoiceno =" + invoiceno + "", con, tran);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("delete from tbl_report2 where invoiceno =" + invoiceno + "", con, tran);
cmd.ExecuteNonQuery();
tran.Commit();
}
catch
{
tran.Rollback();
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}

Best,
Sudheep.



Responses

Posted by: Sudheep.grandhe on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,


private void btn_add_Click(object sender, EventArgs e)
{
char sepChar = '/';
string strondate, strondate1, ondate;
string strinvoicedate, strinvoicedate1, invoicedate2;
//SqlConnection con = new SqlConnection("data source=mars-104;intial catalog=marsweb.imageimpacts;integrated security=true");
DBConnection db = new DBConnection();

SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("insert into tbl_temp (invoiceno,particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid)values(@invoiceno,@particulars,@quantity,@perprice,@amount,@orderno,@compname,@invoicedate,@ondate,@invoiceid)", con);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@particulars", cmb_particulars.Text);
cmd.Parameters.AddWithValue("@quantity", txt_quantity.Text);
cmd.Parameters.AddWithValue("@perprice", txt_perprice.Text);
cmd.Parameters.AddWithValue("@amount", txt_amount.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
if (txt_invoicedate.Text != null && txt_invoicedate.Text != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
cmd.Parameters.AddWithValue("@invoiceid", txt_invoiceno.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
con.Close();
txt_quantity.Text = "";
txt_perprice.Text = "";
txt_amount.Text = "";
netamount();
loadparticularsdata();
vatamount();
griddisplay();
}

private void btn_update_Click_1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_report2 set compname=@compname,compaddress=@compaddress,orderno=@orderno,ondate=@ondate,invoicedate=@invoicedate,amountstatus=@amountstatus,netamount=@netamount,tamount=@tamount where invoiceno=@id", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@id", id);
if (txt_invoicedate.Text != null && txt_invoicedate.Text != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
cmd.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd.Parameters.AddWithValue("@netamount", txt_netamount.Text);
cmd.Parameters.AddWithValue("@tamount", txt_totalamount.Text);
con.Open();
cmd.ExecuteNonQuery();
con .Close ();
SqlConnection con1 = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd1 = new SqlCommand("update tbl_report1 set compname=@compname,orderno=@orderno,invoicedate=@invoicedate,ondate=@ondate where invoiceid=@id", con1);
cmd1.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd1.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd1.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd1.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd1.Parameters.AddWithValue("@ondate", txt_ondate.Text);
cmd1.Parameters.AddWithValue("@id", txt_invoiceno.Text);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
txt_amount.Text = "";
txt_customeraddress.Text = "";
txt_invoicedate.Text = "";
txt_invoiceno.Text = "";
txt_netamount.Text = "";
txt_ondate.Text = "";
txt_orderno.Text = "";
txt_perprice.Text = "";
txt_quantity.Text = "";
txt_totalamount.Text = "";
txt_vat.Text = "";
txt_vatpercentage.Text = "";
MessageBox.Show("updated successfully");
}

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

i am trying to add the record it shows the error.Error4'marsweb.imageimpacts.NewInvoice.sqlConnection' is a 'field' but is used like a 'type'

private void btn_add_Click(object sender, EventArgs e)
{
DBConnection db = new DBConnection();
SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("insert into tbl_report1 (invoiceno,particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid)values(@invoiceno,@particulars,@quantity,@perprice,@amount,@orderno,@compname,@invoicedate,@ondate,@invoiceid)", con);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@particulars", cmb_particulars.Text);
cmd.Parameters.AddWithValue("@quantity", txt_quantity.Text);
cmd.Parameters.AddWithValue("@perprice", txt_perprice.Text);
cmd.Parameters.AddWithValue("@amount", txt_amount.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
if (txt_invoicedate.Text != null && txt_invoicedate.Text.Trim() != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text.Trim() != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
sqlConnection conn = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");//error shows here
SqlCommand cmd1 = new SqlCommand("insert into tbl_report2(compname,compaddress,invoiceno,orderno,invoicedate,ondate,amountstatus)values(@compname,@compaddress,@invoiceno,@orderno,@invoicedate,@ondate,@amountstatus)", conn);
cmd1.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd1.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd1.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd1.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd1.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd1.Parameters.AddWithValue("@ondate", txt_ondate.Text);
cmd1.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd1.Parameters.AddWithValue("@amountstatus", rb_unpaid.Checked);
conn.open();
cmd1.ExecuteNonQuery();

cmd.Parameters.AddWithValue("@invoiceid", txt_invoiceno.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
con.Close();
txt_quantity.Text = "";
txt_perprice.Text = "";
txt_amount.Text = "";
netamount();
loadparticularsdata();
vatamount();
griddisplay();
}

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try below code

private void btn_add_Click(object sender, EventArgs e)

{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
SqlCommand cmd = new SqlCommand("insert into tbl_report1 (invoiceno,particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid)values(@invoiceno,@particulars,@quantity,@perprice,@amount,@orderno,@compname,@invoicedate,@ondate,@invoiceid)", con);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@particulars", cmb_particulars.Text);
cmd.Parameters.AddWithValue("@quantity", txt_quantity.Text);
cmd.Parameters.AddWithValue("@perprice", txt_perprice.Text);
cmd.Parameters.AddWithValue("@amount", txt_amount.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
if (txt_invoicedate.Text != null && txt_invoicedate.Text.Trim() != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text.Trim() != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
cmd.Parameters.AddWithValue("@invoiceid", txt_invoiceno.Text);
cmd.ExecuteNonQuery();

//SqlConnection conn = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");//error shows here
SqlCommand cmd1 = new SqlCommand("insert into tbl_report2(compname,compaddress,invoiceno,orderno,invoicedate,ondate,amountstatus)values(@compname,@compaddress,@invoiceno,@orderno,@invoicedate,@ondate,@amountstatus)", con);
cmd1.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd1.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd1.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd1.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd1.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd1.Parameters.AddWithValue("@ondate", txt_ondate.Text);
cmd1.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd1.Parameters.AddWithValue("@amountstatus", rb_unpaid.Checked);
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
cmd1.ExecuteNonQuery();

con.Close();
txt_quantity.Text = "";
txt_perprice.Text = "";
txt_amount.Text = "";
netamount();
loadparticularsdata();
vatamount();
griddisplay();
}


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

griddisplay()
{
//SqlConnection con = new SqlConnection("data source=mars-104;intial catalog=marsweb.imageimpacts;integrated security=true");
DBConnection db = new DBConnection();

SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("select invoiceno,particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate from tbl_temp", con);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@particulars", cmb_particulars.Text);
cmd.Parameters.AddWithValue("@quantity", txt_quantity.Text);
cmd.Parameters.AddWithValue("@perprice", txt_perprice.Text);
cmd.Parameters.AddWithValue("@amount", txt_amount.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
GridView1.DataSource = dt;

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

try this

public void griddisplay()
{
SqlConnection con = new SqlConnection("data source=mars-104;intial catalog=marsweb.imageimpacts;integrated security=true");
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbl_report1", con); // u can both table if u need any other table colum too
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
GridView1.DataSource = dt;

}


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response