We help IT Professionals succeed at work.

C# Help With IF Value = 0 Do Something

jasont09
jasont09 asked
on
Medium Priority
324 Views
Last Modified: 2013-12-16
Now I know the Title sounds like I should know the answer. But here is my problem and I dont know how to code it

I am fetching an xml response, Most of the time DS.Tables["Price"] is returned in the response. But if its not returned my update fails.

DS.Tables["Price"] is only actually returned if the value = 1 in DS.Tables["Offers"]
Row["TotalOffers"]


if the value = 0  my script crashes.

please can someone explain / show me how I can change my code below to check for it and put an else statement in if the value  = 0


for (int i = 0; i < DS.Tables["Price"].Rows.Count; i++)
          {
              string s6 = DS.Tables["Price"].Rows[i]["FormattedPrice"].ToString().Replace("£", "").Replace(",", "");
              string s7 = "1";// DS.Tables["Price"].Rows[i]["OfferListing_Id"].ToString();
              string n70 = (s11); 
              string sqld = string.Format("update dbo.price set Price ='"+ s6 +"' where productid='" + n70 +"'");
             

              SqlConnection con = new SqlConnection();
              con.ConnectionString = "Data Source=xxx;Initial Catalog=xxx;User Id=xxx; Password=xxx";
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = con;
              cmd.Connection.Open();
              cmd.CommandText = sqld;
              cmd.ExecuteNonQuery();
              cmd.Connection.Close();

          }

Open in new window

Comment
Watch Question

Commented:
Try something like this:

for (int i = 0; i < DS.Tables["Price"].Rows.Count; i++)
{
  
  int x = Convert.ToInt32(DS.Tables["Offers"].Rows[???][???]);

  if (x == 1)
   {
             string s6 = DS.Tables["Price"].Rows[i]["FormattedPrice"].ToString().Replace("£", "").Replace(",", "");
              string s7 = "1";// DS.Tables["Price"].Rows[i]["OfferListing_Id"].ToString();
              string n70 = (s11); 
              string sqld = string.Format("update dbo.price set Price ='"+ s6 +"' where productid='" + n70 +"'");
             

              SqlConnection con = new SqlConnection();
              con.ConnectionString = "Data Source=xxx;Initial Catalog=xxx;User Id=xxx; Password=xxx";
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = con;
              cmd.Connection.Open();
              cmd.CommandText = sqld;
              cmd.ExecuteNonQuery();
              cmd.Connection.Close();
   }

}

Open in new window

Author

Commented:
Thanks for the help RIck

I get this error,

CS1502: The best overloaded method match for 'System.Data.DataRowCollection.this[int]' has some invalid arguments

on this line

int x = Convert.ToInt32(DS.Tables["Offers"].Rows["TotalOffers"]);


Your example gave

int x = Convert.ToInt32(DS.Tables["Offers"].Rows[???][???]);

I'm not sure what I should be putting in the 2nd box [???]

I replaced the 1st one with ["TotalOffers"] which is the one which will =1

can you advise please

Commented:
Hi jasont09,

int x = Convert.ToInt32(DATA_SOURCE.Tables[TABLE_NAME].Rows[ROW_INDEX].[COLUMN]);

Author

Commented:
Thanks Rick.... really appreciate your help

just had to change it round like this,

 int x = Convert.ToInt32(DS.Tables["Offers"].Rows[0]["TotalOffers"]);

  if (x == 1)
   {
       for (int i = 0; i < DS.Tables["Price"].Rows.Count; i++)
          {

Author

Commented:
Thanks for your help Rick

Author

Commented:
Sorry Rick,

been a long day, was accepting your last comment as an A grade, but must have clicked my last comment by mistake. have requested moderator to assist.

the answer was complete, thanks again

Commented:
No problem. I'm glad your code is working now.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.