Reputation: 413
I would like to send multiple records to a website after reading the records from a database table. I can send a single record, but cannot loop through the records and send one at a time.
here is part of the code i am using:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
SqlCommand cmd = new SqlCommand("select top 10 field1, field2, field3, field4 prices", conn);
SqlDataReader rd = cmd.ExecuteReader();
WebResponse rs;
string strNewValue;
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
while (rd.Read())
{
strNewValue = "field1='" + rd[0].ToString() + "'&field2='" + rd[1].ToString() + "'&field3='AM'&field4=" + rd[2].ToString();
stOut.Write(strNewValue);
}
stOut.Close();
rs = req.GetResponse();
this code only posts the last record. How can I loop through the records and post all of them one at a time.
Thanks in advance
Upvotes: 1
Views: 448
Reputation: 6216
It looks like you're putting all the records into the same http post - try changing it to post each record - something like this:
SqlCommand cmd = new SqlCommand("select top 10 field1, field2, field3, field4 prices", conn);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
WebResponse rs; string strNewValue;
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
strNewValue = "field1='" + rd[0].ToString() + "'&field2='" + rd[1].ToString() + "'&field3='AM'&field4=" + rd[2].ToString();
stOut.Write(strNewValue);
stOut.Close();
rs = req.GetResponse();
}
rd.Close();
Upvotes: 2