Shah Rukh Shah Rukh - 7 months ago 9
SQL Question

assign a default value to a variable and change into database

i have a method

getlisting(string url,int ID)


i am passing this parameter ID to another function in another class

Controller __cc = new Controller();
int Status = 1;
__cc.UpdatePaging(ID, Status);


i am passing this to update function and want to assign a default value of zero to ID and 1 to status and then return it and make changes in database,

public void UpdatePaging(int ID,int Status)
{

if (ID != null)
{


ID = 0;
}
else
{
ID = 0;
}

SqlCommand cmd = new SqlCommand("UPDATE Paging SET Status='1' WHERE ID = @ID", obj.openConnection());
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@Status", SqlDbType.Int).Value = 1;
cmd.Parameters.AddWithValue("@ID", SqlDbType.Int).Value = ID;
cmd.ExecuteNonQuery();


}


but i can't do it, is there something wrong with my code? or tell me how to assign a 0 value to int and update it in Database, whenever i get any value from getlisting method it should be assigned zero and update in DB any help?

Answer

based on chat you want this:

public void UpdatePaging(int ID,int Status)
    {

        SqlCommand cmd = new SqlCommand("UPDATE Paging SET Status='1' WHERE ID = @ID", obj.openConnection());
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", SqlDbType.Int).Value = ID;
        cmd.ExecuteNonQuery();

    }

this will set the status to 1 based on the id that is sent to the function

Note your code says SET Status='1' you might mean SET Status=1

You could also make your code more generic like this:

public void UpdatePaging(int ID,int Status=1)
{
  SqlCommand cmd = new SqlCommand("UPDATE Paging SET Status=@Status WHERE ID = @ID", obj.openConnection());
  cmd.CommandType = System.Data.CommandType.Text;
  cmd.Parameters.AddWithValue("@ID", SqlDbType.Int).Value = ID;
  cmd.Parameters.AddWithValue("@Status", SqlDbType.Int).Value = Status;
  cmd.ExecuteNonQuery();
}