Click here to Skip to main content
13,090,178 members (49,794 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Want Query to Lock one process..
Wen we go to next month process first we want to lock previous month calculation by creating settings..
This should be for whole table we have set common lock procedure.
Need query for Sql2005
Posted 5-Mar-12 4:16am
Mark Nischalke 5-Mar-12 10:38am
Your question is not clear
BobJanova 5-Mar-12 11:24am
You can lock tables within a single transaction trivially. I think you mean permanently locking a whole table as a read only archive, in which case you should remove the UPDATE and DELETE (and ALTER etc) privileges from the application user on that table.
Charuroopa 5-Mar-12 11:33am
not asking for permanent locking..
if we want to calculate next month calculation first we have to lock previous once right? for that i need query..
and also we want to lock the financial year while we move to next year process..
Whether we want to maintain one separate table for this process or how we have to proceed it further..
Charuroopa 6-Mar-12 1:06am
What i m doing is payroll project...
For that i am asking..
i hope now u wil come to know for what i am asking for...
Charuroopa 7-Mar-12 1:08am
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Security;

namespace lockprocedure
public partial class _Default : System.Web.UI.Page

SqlDataReader reader;
SqlConnection conn;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)


protected void Button1_Click(object sender, EventArgs e)
string connectionString="Data Source=SNSWAMY\\SQLEXPRESS;Initial Catalog=payroll;Persist Security Info=True;User ID=******;Password=*********";
conn = new SqlConnection(connectionString);
string updateSql = "update lock_table set key_value=1 where sal_date='2012-02-25 00:00:00.000'";
cmd = new SqlCommand(updateSql, conn);


catch (Exception)

MessageBox("Locked the previous month calculation");
private void MessageBox(string message)
if (!string.IsNullOrEmpty(message))
Response.Write("<script type=\'text/javascript' language=\'javascript\'>");
Response.Write("alert('" + message + "');");

protected void Button2_Click(object sender, EventArgs e)
int key_value;
string connectionstring="Data Source=SNSWAMY\\SQLEXPRESS;Initial Catalog=payroll;Persist Security Info=True;User ID=******;Password=******";
conn = new SqlConnection(connectionstring);
// string updateSql = "update salary set key_value=1 where process_datetime='2012-02-25 00:00:00.000'";
SqlCommand cmd = new SqlCommand(("select key_value from lock_table where sal_date='2012-02-25 00:00:00.000' "), conn);
// cmd = new SqlCommand(updateSql, conn);
cmd.Connection = conn;

reader = cmd.ExecuteReader();


/*if (reader.HasRows)
while (reader.Read())
// key_value = reader.GetOrdinal("key_value");
//key_value = reader.GetOrdinal("key_value");
/* if (key_value == 0)
MessageBox("Lock the previous Month Calculation");
MessageBox("Can proceed further");


key_value = reader.GetOrdinal("key_value");
// int key_value =reader.GetInt32(reader.GetOrdinal("key_value"));

if (key_value == 0)
MessageBox("Lock the previous Month Calculation");
MessageBox("Can proceed further");



This is my code for lock procedure.. In this i am getting an error invalid attempt to read when no data is present. c# and also reader is not working properly because after i locked the previous month calculation again it is asking to lock

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This is ENTIRELY dependant on your business requirements, procedures, and your database design. What you do is entirely up to you. You don't NEED to "lock" anything unless your design requires it, depending on what your definition of "lock" is.

It may be as simple as taking the records from the production tables and moving them to archive tables of some kind. I, and nobody here, has any idea of what your requirements and design are, so it's impossible to say exactly.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 5 Mar 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100