Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I was trying to update/Insert with sqlcommandBuilder but was getting problem after updating 20 record in 21st there was some sql exception(may be data type,range of value.....) the 21st record was not updating because of some sql exception.because of this all other record was getting rollback.Please guide me to solve this problem.
I am supposing if exception will come then how to handle with out effecting the transaction to rollback.


Any exception x,y,z.I want before exception what ever i inserted/Updated that should persist in data base How to handle this Please Guide me guys

Copied from a comment:
C#
string TableName = "EmpDat";
DataTable orgTable = new DataTable();
orgTable.TableName=TableName;
orgTable.Columns.Add("EmpName", typeof(string));
orgTable.Columns.Add("Desig", typeof(string));
orgTable.Columns.Add("EmpFname", typeof(string));
orgTable.Columns.Add("Salary", typeof(int));
orgTable.Columns.Add("Sex", typeof(string));
orgTable.Columns.Add("Branch", typeof(string));
DataRow dr = orgTable.NewRow();
dr["EmpName"] = "Jack";
dr["Desig"] = "Chawki Daar";
dr["EmpFname"] = "Khain The";
dr["Salary"] = 12;
dr["Sex"] = "H";
dr["Branch"] = "Not Allowed";
orgTable.Rows.Add(dr);
SqlTransaction tran;
SqlConnection con = new SqlConnection("Constr");
con.Open();
tran = con.BeginTransaction();
SqlCommand cmd = new SqlCommand("select * from EmpDetail", con);
cmd.Transaction = tran;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"EmpDat");
DataRow[] dr1 = ds.Tables[0].Select("empname='Jack'");
dr1[0]["empName"] = "Good Morning";
orgTable.AcceptChanges();
DataTable f = orgTable.GetChanges(DataRowState.Modified);
// con.Open();
SqlCommandBuilder sqlcmdBuild = new SqlCommandBuilder(da);
da.Update(ds.Tables[0]);
Posted
Updated 23-Aug-12 17:35pm
v5
Comments
[no name] 23-Aug-12 3:38am    
What is the exception you are facing.
write the exception first and code as well where you are facing the exception
Himanshu Yadav 23-Aug-12 3:55am    
string TableName = "EmpDat";
DataTable orgTable = new DataTable();
orgTable.TableName=TableName;
orgTable.Columns.Add("EmpName", typeof(string));
orgTable.Columns.Add("Desig", typeof(string));
orgTable.Columns.Add("EmpFname", typeof(string));
orgTable.Columns.Add("Salary", typeof(int));
orgTable.Columns.Add("Sex", typeof(string));
orgTable.Columns.Add("Branch", typeof(string));
DataRow dr = orgTable.NewRow();
dr["EmpName"] = "Jack";
dr["Desig"] = "Chawki Daar";
dr["EmpFname"] = "Khain The";
dr["Salary"] = 12;
dr["Sex"] = "H";
dr["Branch"] = "Not Allowed";
orgTable.Rows.Add(dr);
SqlTransaction tran;
SqlConnection con = new SqlConnection("Constr");
con.Open();
tran = con.BeginTransaction();
SqlCommand cmd = new SqlCommand("select * from EmpDetail", con);
cmd.Transaction = tran;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"EmpDat");

DataRow[] dr1 = ds.Tables[0].Select("empname='Jack'");
dr1[0]["empName"] = "Good Morning";

orgTable.AcceptChanges();

DataTable f = orgTable.GetChanges(DataRowState.Modified);


// con.Open();
SqlCommandBuilder sqlcmdBuild = new SqlCommandBuilder(da);
da.Update(ds.Tables[0]);
[no name] 23-Aug-12 14:05pm    
"some exception" is not at all helpful.
Himanshu Yadav 23-Aug-12 23:34pm    
Hi,
Means any exception x,y,z.I want before exception what ever i inserted/Updated that should persist in data base How to handle this Please Guide me guys
Ali91Asadi 23-Aug-12 16:33pm    
Did you write this code with parameters!!!!!!!!!(SQL Injection)
Search in Library.

command.CommandText="SELECT * FROM tbl_book WHERE Book_Name LIKE N'%"+ txt_book_name.Text +"%'"+
" AND Book_Latin_Name LIKE N'%"+txt_book_latin_name.Text+"%'";

1 solution

Try using Try catch and then commit your SqlTransaction in catch block

something like

C#
try
{
  //Your code here;
}
Catch(Exeception ex)
{
 //commit your SqlTransaction here;
}
 
Share this answer
 
v2
Comments
Himanshu Yadav 5-Sep-12 8:43am    
Hi,
ok i agree with u but after exception all the updation will rollback so how to control that?
CS2011 6-Sep-12 13:55pm    
Not if you commit it. If you commit it it will save the all the data what was inserted in the table till that point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900