Thread with SqlDataAdapter not working, plz help

muralik

Member
Joined
Jun 14, 2003
Messages
5
Hi,

I tried fetching a part of data from the database and tried to update it using thread, it is not working as I expected.
The result I got in the browser is as follows:
********
The Main() thread calls this after starting the new InstanceCaller threads.
Inside InstanceMethod
SQL 1 : SELECT temp_id, temp_senddate FROM temp_mails WHERE company_id = 1 AND temp_send = N
********

It is not moving ahead and displaying(details which has to bee displayed):
*************
RECORDCOUNT 1 : 3
sTempID : 1
sTempID : 2
sTempID : 3
End of function
*************

Below is the snapshot of my code:

C#:
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;

namespace nnews2
{
	public class nThread1 : System.Web.UI.Page
	{
		public SqlConnection objConnection;
		
		string strConn = "server=(local)\\netsdk;database=Test;Trusted_Connection=yes";
		string sTempID = "";
		string sSql = "";
		string sSQLCommand = "";

		int iCount = 0;
		private void Page_Load(object sender, System.EventArgs e)
		{
			objConnection = new SqlConnection(strConn);
			objConnection.Open();

			Thread InstanceCaller = new Thread(new ThreadStart(InstanceMethod));
Response.Write("The Main() thread calls this after starting the new InstanceCaller threads.<BR>");

			InstanceCaller.Priority = ThreadPriority.Highest;
			InstanceCaller.Start();
		}

		public void InstanceMethod()
		{
Response.Write ("Inside InstanceMethod <BR>");
			SqlDataAdapter objTempDA = new SqlDataAdapter();
			sSql = "SELECT temp_id, temp_senddate FROM temp_mails ";
			sSql += "WHERE company_id = 1 AND temp_send = N";
Response.Write ("SQL 1 : " + sSql + "<BR>");

			SqlDataAdapter objDA = new SqlDataAdapter(sSql, objConnection);
			DataSet objDS = new DataSet();
			objDA.Fill (objDS, "temp_mails");
			DataTable objDT = objDS.Tables[0];
Response.Write ("RECORDCOUNT 1 : " + objDT.Rows.Count + "<BR>");
			if (objDT.Rows.Count > 0)
			{
				foreach (DataRow objDR in objDT.Rows)
				{
					iCount++;
					sTempID = objDR["temp_id"].ToString();
					sSQLCommand = "UPDATE temp_mails set temp_send=Y WHERE company_id=1 AND temp_id = " + sTempID + "";

					objTempDA.UpdateCommand = new SqlCommand(sSQLCommand, objConnection);
					objTempDA.UpdateCommand.ExecuteNonQuery();
Response.Write ("sTempID : " + sTempID + "<BR>");
				}
			}
Response.Write ("End of function<BR>");
		}
	}
}

Where I am doing wrong.
NOTE: I commented the thread part and just called the function InstanceMethod(), it worked fine.

Please help me.

Murali.
 
Last edited by a moderator:
Back
Top