Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here i have used function but the data is not updated in databse
please tell me where i go wrong


VB
public static DataTable test(string code)
        {
           
            SqlConnection con = new SqlConnection("Data Source=SHILPA;Initial Catalog=TreeDB;Integrated Security=True");
            SqlCommand com = new SqlCommand();
            con.Open();
            DataTable dt = new DataTable();
            try
            {
               
                string queryRM = "SELECT * FROM Registration_Master WHERE sponcorid='" + code.ToString() + "'";
                dt = DBHelper.SQLHelper.getTable(queryRM.ToString());
                if (dt.Rows.Count > 0)
                {

                    DataTable dtl = new DataTable();
                    string left = "SELECT * FROM Registration_Master WHERE side='LEFT'";
                    dtl = DBHelper.SQLHelper.getTable(left.ToString());

                    int count_mem = Convert.ToInt32(dtl.Rows[0]["count_mem"]);
                    count_mem++;

                    com.Connection = con;
                    com.CommandText = "UPDATE Counter SET leftcount =" + count_mem + "";
                    com.CommandType = CommandType.Text;
                    com.ExecuteNonQuery();

                    DataTable dtr = new DataTable();
                    string right = "SELECT * FROM Registration_Master WHERE side='RIGHT'";
                    dtr = DBHelper.SQLHelper.getTable(right.ToString());
                    if (dtr.Rows.Count > 0)
                    {
                        int count1 = Convert.ToInt32(dtr.Rows[0]["count_mem"]);
                        count1++;
                        com = new SqlCommand();
                        com.Connection = con;
                        com.CommandText = "UPDATE Counter SET rightcount =" + count1 + "";
                        com.CommandType = CommandType.Text;
                        com.ExecuteNonQuery();
                    }
                    DataTable dtcount = new DataTable();
                    string query = "SELECT * FROM Counter";
                    dtcount = DBHelper.SQLHelper.getTable(query.ToString());
                    if (dtcount.Rows.Count > 0)
                    {
                        if (dtcount.Rows[0]["leftcount"] == dtcount.Rows[0]["rightcount"])
                        {
                            int count2 = Convert.ToInt32(dtl.Rows[0]["count_mem"]);
                            count2++;
                            com = new SqlCommand();
                            com.Connection = con;
                            com.CommandText = "UPDATE Counter SET sponcorcount = " + count2 + "";
                            com.CommandType = CommandType.Text;
                            com.ExecuteNonQuery();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }

        
            return dt;
        }
Posted
Updated 7-Nov-12 19:50pm
v2
Comments
sahabiswarup 8-Nov-12 2:10am    
are you getting any error in connection? and what about where clause in Update command?
sumit kausalye 8-Nov-12 2:18am    
actually i dont want any where clause because i want whole leftcount or rightcount updated not an single rows thats why i remove where clause... any no any error just my table records are blank
Bala Selvanayagam 8-Nov-12 2:12am    
what is in DBHelper?
sumit kausalye 8-Nov-12 2:19am    
the DBHelper is class i have taken where i bind database connection to the front end
sahabiswarup 8-Nov-12 2:26am    
you may try once the query directly to SQL SSME so that you'll get atleast an idea that your query is working properly.

1 solution

"any no any error just my table records are blank" - ehm, do you want to say that there is no entry in your Counter table? Then of course an UPDATE query cannot have any effect: an entry must exist before it can be updated. Use INSERT in such a case.
Or were you struck by your if onditions (e.g. if (dt.Rows.Count > 0)): if your tables are empty, Rows.Count will be 0.
 
Share this answer
 
Comments
sumit kausalye 8-Nov-12 2:57am    
i did that ya now records are inserted but i want the leftcount and rightcount should i increment like for example
if record added then it should give leftcount as 1 next if added it should give 2 and so no..

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