1

How to increase totaldownloads value in my database file code is given below

SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    DataRow dr;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            lblMessage.Visible = true;
            GridView1.Visible = false;
            //AppContent.Visible = false;
        }
        else
        {
            if (!Page.IsPostBack)
            {
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
                ArrayList myArrayList = ConvertDataSetToArrayList();
                Literal objliteral = new Literal();
                StringBuilder objSBuilder = new StringBuilder();

                //Add some column to datatable display some products           
                dt.Columns.Add("appImg");
                dt.Columns.Add("appName");
                dt.Columns.Add("appLink");
                dt.Columns.Add("appType");
                dt.Columns.Add("TotalVisitors");
                dt.Columns.Add("TotalDownloads");
                dt.Columns.Add("RemainingVisitors");

                // Display each item of ArrayList
                foreach (Object row in myArrayList)
                {
                    //Add rows with datatable and bind in the grid view
                    dr = dt.NewRow();
                    dr["appImg"] = ((DataRow)row)["AppImg"].ToString();
                    dr["appName"] = ((DataRow)row)["AppName"].ToString();
                    dr["appLink"] = ((DataRow)row)["AppLink"].ToString();
                    dr["appType"] = ((DataRow)row)["AppType"].ToString();
                    dr["TotalVisitors"] = "Plan Visitors: " + ((DataRow)row)["TotalVisitors"].ToString();
                    dr["TotalDownloads"] = "Downloaded: " + ((DataRow)row)["TotalDownloads"].ToString();
                    dr["RemainingVisitors"] = "Remaining Visitors: " + ((DataRow)row)["RemainingVisitors"].ToString();
                    dt.Rows.Add(dr);
                }

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }            
        }
    }

    public ArrayList ConvertDataSetToArrayList()
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
        SqlCommand cmd = new SqlCommand();
        if (Session["UserId"] != null && (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) != 2))
        {
            cmd.CommandText = "Select * from tblApp WHERE AppType = '" + Session["UserOSType"] + "' ORDER BY TotalVisitors";
        }
        else
        {
            cmd.CommandText = "Select * from tblApp ORDER BY TotalVisitors";            
        }
        cmd.Connection = sqlcon;
        sqlcon.Open();

        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        DataSet dsApp = new DataSet();
        da.Fill(dsApp, "tblApp");

        ArrayList myArrayList = new ArrayList();
        foreach (DataRow dtRow in dsApp.Tables[0].Rows)
        {
            myArrayList.Add(dtRow);
        }
        sqlcon.Close();
        return myArrayList;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "download")
        {
            Button ib = (Button)e.CommandSource;
            int index = Convert.ToInt32(ib.CommandArgument);
            GridViewRow row = GridView1.Rows[index];            
            Label l2 = (Label)row.FindControl("Label2");
            Label lbTotallVisitors = (Label)row.FindControl("Label4");
            Label lblTotalDownloads = (Label)row.FindControl("Label5");
            Label lblRemainingVisitors = (Label)row.FindControl("Label6");


            string updateSQL = "UPDATE tblUser SET DownloadedApps = '" + lbl + "', Amount = '" + Session["Amount"] + "', TotalAmount='" + Session["TotalAmount"] + "' WHERE Id= '" + Session["UserId"] + "'";
            using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()))
            {
                using (SqlCommand updateCommand = new SqlCommand(updateSQL, sqlConn))
                {
                    sqlConn.Open();
                    updateCommand.ExecuteNonQuery();
                    updateCommand.Connection.Close();

                }
            }            
            Response.Redirect(l2.Text);
        }
    }
4
  • total download must be a int columns why not you just use Update TableName set columnname = columnname + 1 Commented Jul 19, 2013 at 10:50
  • Please explain the scenerio and then post the code. And ask what exactly you need Commented Jul 19, 2013 at 10:54
  • i'm having another issue about update.. Commented Jul 19, 2013 at 18:24
  • link Commented Jul 19, 2013 at 18:25

1 Answer 1

1

UPDATE Table SET Column = Column + 1

Sign up to request clarification or add additional context in comments.

6 Comments

Oh dear answers.. Sometimes you are funny :)
@Soner- Things lacks value as time passes
string updateSQL = "UPDATE tblUser SET DownloadedApps = DownloadedApps + 1 , Amount = Amount + 0.01, TotalAmount= TotalAmount + 1 WHERE Id= '" + Session["UserId"] + "'"; ?
When i try to update two tables i got some error Error : "Conversion failed when converting the varchar value 'System.Web.UI.WebControls.Label' to data type int."
string updateSQL = "UPDATE tblUser SET DownloadedApps = DownloadedApps + 1 , Amount = Amount + 0.01, TotalAmount= TotalAmount + 0.01 WHERE Id = '" + Session["UserId"] + "'" + "UPDATE tblApp SET TotalDownloads = TotalDownloads + 1 , RemainingVisitors = RemainingVisitors - 1 WHERE AppId= '" + lblappName + "'";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.