#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    1
    Rep Power
    0

    Thread with SqlDataAdapter not working, plz help


    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:

    /* CODE STARTS HERE */
    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>");
    }
    }
    }

    /* CODE ENDS HERE */

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

    Please help me.

    Murali.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Romania
    Posts
    9
    Rep Power
    0

    Cool Another way to connect to SQLServer


    You don't need all this work. Near the toolbox there is "Server" from wich you select the database and drag the tables over the form. Then bellow the "properties" you heve something like "generate datase". All that you have to do now is in the Load event for the Form(double clik on the form Text area) to write this code:

    this.sqlconnection.ConnectionString = @" "//for not using the "\\"
    this.sqlconnection.Open();

    //depends on the number of the tables

    this.sqldataadapter1.SelectComand.ExecuteNonQuery();
    this.sqldataadapter2.SelectComand.ExecuteNonQuery();
    ......
    this.sqldataadapter4.SelectComand.ExecuteNonQuery();

    this.sqldataadapter1.Fill(this.myDataSet,"the name of the table");
    this.sqldataadapter2.Fill(this.myDataSet,"the name of the table");

    this.sqldataadapter3.Fill(this.myDataSet,"the name of the table");

    this.sqldataadapter4.Fill(this.myDataSet,"the name of the table");


    this.datagrid.DataSource = this.myDataSet;

    //if you want to bind the tables directly you could do something
    //this.datafrid.DataMember = "the name of the table"

    this.sqlconnection.Close();


    If you are having trouble send an email and i'l send you a piece of code and a database of my one to play with.


    I hope that what I've just said to be in your help.


    :cool:
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Portugal
    Posts
    106
    Rep Power
    11
    What's the point of participating in a forum if you ask people to talk to you through e-mails? that's not the spirit of forums....

IMN logo majestic logo threadwatch logo seochat tools logo