Page view counter

Nested Stored Proecedure

Last post 08-29-2008 5:22 PM by bedubs. 3 replies.

Sort Posts:

  • Nested Stored Proecedure

    08-29-2008, 11:27 AM
    • Loading...
    • bedubs
    • Joined on 04-03-2007, 9:58 AM
    • Tx
    • Posts 84
    • Points 29

    I have a pretty basic connection to a database, My question is nested in the While Statement
    C#.NET
    MSSQL 2005
    Data.SqlClient

     

            String strCon = "Server=...";
            SqlConnection conn = new SqlConnection(strCon);
            SqlDataReader rdr = null;
    
            conn.Open();
    
            SqlCommand cmd = new SqlCommand("select intID, email from cventcontacts WHERE URL is null",conn);
    
            rdr = cmd.ExecuteReader();
    
            while (rdr.Read())
            {
               	// I want to get information from a 3rd party API based on email from rdr[1] then UPDATE the record in my database
    // I think i can figure out the api side of it, but how do i reconnect to the database and update the record?
    } rdr.Close(); conn.Close(); emails.Text = sb.ToString();
     Thanks in advance
  • Re: Nested Stored Proecedure

    08-29-2008, 11:36 AM
    Answer
    • Loading...
    • jameswright
    • Joined on 08-28-2008, 3:56 PM
    • Morgantown, WV
    • Posts 137
    • Points 596

    Actually, you're still connected to the database inside your while loop. So, you would just need to construct the new command you need and and use cmd.ExecuteNonQuery().

    while(reader.Read())
    {
       string val = //Get val from API.

       SqlCommand update = new SqlCommand("UPDATE MyTable ... ");
       update.ExecuteNonQuery();
    }

    ..close up connection. That on the right track?

    jameswright
    www.jwright.info

    "Never be so busy making a living that you forget to make a life."
  • Re: Nested Stored Proecedure

    08-29-2008, 12:00 PM
    • Loading...
    • bedubs
    • Joined on 04-03-2007, 9:58 AM
    • Tx
    • Posts 84
    • Points 29

    YES Thank you jameswright for the quick response. That is exactly what i need. I think i might have something wrong on my syntax because when i execute my script i receive an error : There is already an open DataReader associated with this Command which must be closed first.

  • Re: Nested Stored Proecedure

    08-29-2008, 5:22 PM
    Answer
    • Loading...
    • bedubs
    • Joined on 04-03-2007, 9:58 AM
    • Tx
    • Posts 84
    • Points 29
    I found out the answer I need to add

    MultipleActiveResultSets=True

Page 1 of 1 (4 items)