none
How to use datareader and update table in sql ? Pls help . Urgent RRS feed

  • Question

  • Hi

     

    I am trying to update table while reader reads and If it has row. It works for select command but throwing error when I try to put update command inside the ' while'. I have done the same in vb.net it works fine. But couldn't do in the c#. Please help. Urgent

     

    I am getting the below error message

     

    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0019: Operator '&' cannot be applied to operands of type 'string' and 'int'

    Source Error:

     

    Line 239:                    //OleDb.OleDbCommand WD2 = new OleDb.OleDbCommand();
    Line 240:                                       
    Line 241:                    WDSQL2 = "UPDATE tblcustomerdetails SET downloaded = '" & aftdwnload & "'WHERE dateordered = '" & orddate & "' AND Authcode = '" & pcode & "'";
    Line 242:                //Where ID = '" & ID & "'"
    Line 243:
     

     

     

    Advance thanks

     

    string WDSQL2;

    SqlConnection cn = new SqlConnection(@"database=SRVON;server=ONLINE\SRV01;user=name;pwd=password");

    cn.Open();

    SqlCommand sqlComm = new SqlCommand("SELECT customerDetailsID, dateordered, downloaded FROM tblcustomerdetails WHERE downloaded = 0", cn);

    SqlDataReader r = sqlComm.ExecuteReader();

    if (r.HasRows)

    {

    while (r.Read())

    {

    string orddate = (string)r["dateordered"];

    bool downloaded = (bool)r["downloaded"];

    int pcode = 0;

    int aftdwnload = 1;

    if (downloaded = false)

    //OleDb.OleDbCommand WD2 = new OleDb.OleDbCommand();

    WDSQL2 = "UPDATE tblcustomerdetails SET downloaded = '" & aftdwnload & "'WHERE dateordered = '" & orddate & "' AND Authcode = '" & pcode & "'";

    //Where ID = '" & ID & "'"

     

    //Try

    WD2 = new SqlCommand(WDSQL2, cn);

    WD2.ExecuteNonQuery();

    //Datacon.Close()

    }

    r.Close();

    cn.Close();

    }

     

    Wednesday, April 9, 2008 8:58 AM

Answers

All replies

  • insteat of & you the + sign.
    Wednesday, April 9, 2008 10:14 AM
  • Even better would be to use the string.Format method. Or even build your SQL using the StringBuilder and call the .ToString() to get the output.

    Code Snippet

     

    WDSQL2 = "UPDATE tblcustomerdetails SET downloaded = '" & aftdwnload & "'WHERE dateordered = '" & orddate & "' AND Authcode = '" & pcode & "'"

     

     

     

    Can be expressed as

     

    Code Snippet

    WDSQL2 = string.Format("UPDATE tblCustomerDetails SET downloaded = '{0}' WHERE dateordered = '{1}' AND AuthCode = '{2}'", aftdwnload, orddate, pcode);

     

     

    Thursday, April 10, 2008 2:28 PM