Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using asp.net with c#.
i have a confusion.see the below code.

while (itr.MoveNext())   //this loop will rotate 26 time coz the value of itr is 26
            {
                Hashtable quote = ((Hashtable)(itr.Current));
                Response.Write(("Quote Symbol:" + quote["symbol"]));
                Response.Write(("Title:" + quote["title"]));                //1
                Response.Write(("Time:" + quote["time"]));

                if ((fxfeed.getInterval() == 1))
                {
                    if (fxfeed.getPrice().Equals("bid,ask"))
                    {
                        Response.Write(("Bid:" + quote["bid"]));
                        Response.Write(("Ask:" + quote["ask"]));
                    }
                    else
                    {
                        Response.Write(("Price:" + quote["price"]));
                    }
                }
                else
                {
                    Response.Write(("Open:" + quote["open"]));        //2
                    Response.Write(("High:" + quote["high"]));        //3
                    Response.Write(("Low:" + quote["low"]));        //4
                    Response.Write(("Close:" + quote["close"]));        //5
                }

as i commented that the while loop will rotate 26 times. so each time lines which are represented by number comments(//1,//2 etc) these will be printed on screen. it means the at the completion of the while loop 107 values will be printed(which are represented by comments numbers). i have a database and want to store those 107 values in 107 cols in a database table. so can somebody please tell me where to put the sql command to insert the data and what do i need to write in that command. please help me.
Posted

1 solution

You need to capture all the numbers on the loop before inserting it on the database. You don't want to do the database update inside the loop since this, I think, might cause an overhead on your database. What you can probably do is insert a logic that will get all the numbers while loop is running and store it on a collection. And then, put the values of the collection as parameters on an SqlDataCommand, and then do the database update.

[Update to OP's follow up question]

Here is one way you might want to do. Basically, inside the loop, you add all the parameters in a collection, like a dictionary, for example.
C#
Dictionary<string, string> param = new Dictionary<string, string>();
while(itr.MoveNext())
{
   param.Add("@parameterName", "value"); // take note that you might execute this method more than once per execution of a loop, depending on what values you need to insert to your database.
   //other logic
}


After gathering all the required parameter values, you now add them on the parameters of your SqlCommand. See the following example.

C#
string query = "INSERT INTO YourTable (col1, col2, col3, etc....) VALUES(@val1, @val2, @val3, etc...)";
          SqlConnection conn = new SqlConnection("Your connection string");
          SqlCommand comm = new SqlCommand(query, conn);
          foreach (KeyValuePair<string, string> keyVal in param)
          {
              comm.Parameters.AddWithValue(keyVal.Key, keyVal.Value);
          }
          comm.ExecuteNonQuery();

Of course this will not work for you. You have to tailor it to suit your scenario. But I hope you get the idea now.
 
Share this answer
 
v2
Comments
saifullahiit 18-Jul-11 9:54am    
but i need to insert 104 values in database at the completion of loop how is it possible.
walterhevedeich 18-Jul-11 10:07am    
Are you going to insert at one table? What's your column names look like?
saifullahiit 18-Jul-11 10:11am    
yes i am going to insert it in one table.do i need to insert it in different tables? name of cols are AUD/USD_HIGH,AUD/USD_OPEN etc.
walterhevedeich 18-Jul-11 22:46pm    
do i need to insert it in different tables?
I think you need to normalize your database first. Having a table with 107 columns is more or less, not normal. If, for any reason, you cant seem to change the structure of your tables, see my updated answer for an idea on how you can do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900