Archive for ‘ADO.NET’

July 26, 2010

Error: This row already belongs to another table

I came across a situation where i had to loop through one of the DataTable and to import the same data into another datable where the data order is different. I was trying to do it but the data is not getting copied to the new DataTable. What I have found out is that you can’t just add rows from one DataTable to another. Initially the table structure must be Cloned and then the rows has to be imported. Cloning the DataTable clones the structure including all DataTable schemas and constraints.

DataTable newTable = new DataTable("Sorted");
newTable = transactionHistoryTable.Clone();
for (int j = transactionHistoryTable.Rows.Count - 1; j >= 0; j—)
{
newTable.ImportRow(transactionHistoryTable.Rows[j]);
}
_dsResponse.Tables.Add(newTable);

as a summary what i am doing is, initially the data what i have is some data in some order which was populated from AS400 API. I need the same data other way around, bottom to top (For ex: transactionHistoryTable has some data and i need to traverse it back).

Advertisements
October 20, 2008

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

One of strange error that i had to deal with is the one mentioned above. The error used to prompt more frequently when the number of records increased in table. So at the initial stage of developments you won’t have this timeout error. But when the system is in operation level and when the production data increases you may haev to come across this timeout error.

There are two main Timeout property in ADO.NET.

1. Connection Timeout for Connection
2. Timeout for Data Access( Command Object or DataAdapter)

Setting Connection Timeout property for SQLConnnection object ?

You can’t set timeout property in connection object because its a READ ONLY property.

The proper way to do this is to set the timeout value in connection string as mentioned below.

SqlConnection conn = new SqlConnection(“server=Server;uid=sa;pwd=123456;database=myDB;Connection Timeout=120”);

Setting the timeout property in DataAdapter or CommandObject.

SqlDataAdapter sqlAdapter = new SqlDataAdapter();
sqlAdapter.SelectCommand = new SqlCommand(sqlQuery, conn);
sqlAdapter.SelectCommand.CommandTimeout = 0;

Setting the timeout value to 0 will makes adapter or command to wait for indefinite time before terminating the attempt to execute a command and generating an error. The default timeout value for connection object is 15 seconds.

hope this might help you to solve the problem.

February 2, 2007

ADO.NET Connection Strings

ODBC Driver for Excel

strConnection = _T(“Driver={Microsoft Excel Driver (*.xls)};DriverId=790;” bq=C:\\DatabasePath\\DBSpreadSheet.xls;

DefaultDir=c:\\databasepath;”);

ODBC Driver for Text
strConnection = _T(“Driver={Microsoft Text Driver (*.txt; *.csv)};” “Dbq=C:\\DatabasePath\\;Extensions=asc,csv,tab,txt;”);

Refer Connectionstrings