Click here to Skip to main content
15,902,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;
using System.Text;

public partial class Nampak_AddFal : System.Web.UI.Page
{
    string selectSQL, selectSQL1, selectSQL5;
    string updateSQL, updateSQL1, updateSQL5, updateSQL6, updateSQL2;
    SqlCommand cmd = new SqlCommand();
    SqlCommand cmd1 = new SqlCommand();
    SqlCommand cmd2 = new SqlCommand();
    SqlCommand cmd3 = new SqlCommand();
    SqlCommand cmd5 = new SqlCommand();
    SqlCommand cmd6 = new SqlCommand();
    SqlCommand cmd9 = new SqlCommand();
    SqlConnection dbConn = new SqlConnection();
    SqlConnection dbConn1 = new SqlConnection();
    SqlConnection dbConn2 = new SqlConnection();
    SqlConnection dbConn3 = new SqlConnection();
    SqlConnection dbConn4 = new SqlConnection();
    SqlConnection dbConn5 = new SqlConnection();
    SqlConnection dbConn6 = new SqlConnection();
    SqlDataReader dr, dr1, dr2, dr3, dr5, dr8;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SetSS_Click(object sender, EventArgs e)
    {
        Session["YY"] = DropDownList1.SelectedItem.Text;
        int Q = Convert.ToInt32(TextBox1.Text);
        


        selectSQL = "select id, RTicket from Staff WHERE Jobtype ='Contract'";
        dbConn.ConnectionString = "Data Source=LAPTOP\\SQLEXPRESS; Initial         Catalog=Namh;Integrated Security=True;";
        cmd.Connection = dbConn;
        cmd.CommandText = selectSQL;
        cmd.CommandType = CommandType.Text;

        try
        {
            dbConn.Open();
         
            dr = cmd.ExecuteReader();
           while (dr.Read())
            {
                Session["FT"] = dr["Rticket"].ToString();
                 int W = Convert.ToInt32(Session["FT"]);
                int S = W + Q;
                string P = S.ToString();
                Session["PP"] = S.ToString();
             


              updateSQL1 = "update Staff set  Rticket='" + Session["PP"].ToString() + "' where  Jobtype ='Contract' ";
              // Response.Write(updateSQL1);
               dbConn1.ConnectionString = "Data Source=LAPTOP\\SQLEXPRESS; Initial Catalog=Namh;Integrated Security=True;";

               cmd1.Connection = dbConn1;
               cmd1.CommandText = updateSQL1;
               cmd1.CommandType = CommandType.Text;
               try
               {

                   dbConn1.Open();
                   int updated1 = cmd1.ExecuteNonQuery();
                   if (updated1 == 1)
                   {
                      Response.Write("well done");
                       


                   }
                   else
                   {
                     Response.Write("double");
                       


                       
                       } 
                       
                      
                   
               }
               catch (Exception err)
               {
                   Label6.Text = "Error  Logging in 2 ";
                   Label6.Text += err.ToString();
               }
               finally
               {
                   dbConn1.Close();
               }

            }
           
            dr.Close();
        }
        catch (Exception err)
        {
            Label6.Text = "Error  Logging in 2 ";
            Label6.Text += err.ToString();
        }
        finally
        {
            dbConn.Close();
        }



    }
}


my problem is that i am able to read values before adding a value and lastly updating it in the database e.g

20
34
56
67

but it updates all the values to same values in the database e.g

98
98
98
98

instead of say int Q =4

24
38
60
71
in the database ,please help

What I have tried:

i have tried using the update statement
Posted
Updated 18-Feb-18 15:35pm
v3

1 solution

This is your SQL statement:
SQL
update Staff set  Rticket='" + Session["PP"].ToString() + "' where  Jobtype ='Contract'


So, it is pretty clear that it will update any record in the Staff table that has a jobtype of Contract. So, if you don't want to update all the Contract records you need to add something to your WHERE clause.

Also, some pointers:
1. You did not show all your code but you have lots of separate sql connections and sql commands. This is likely a bad way of doing things. You mention in your title about a while loop but you don't need it. It looks like you are pulling out a value, changing it and then writing back to the db. Use only a single connection for this process and a single sql command.

2. Name your controls something that makes sense. It looks like you show messages in a control named Label6. Name it lblMessages or lblError or something like that so that you don't have to switch over to your form design every time you try and figure out what that control is.
 
Share this answer
 

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