Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with C# window forms application which saves data into database and I need to load same database onto form while user clicks on search button.

I have problem with assigning doc_seq_num which plays the key role to populate the fields with the latest data.

Suppose I saved original data doc_seq_num saved as 1 and then data gets changed and doc_seq_num increments to 1.

Now the stored procedure I have is meant to populate the fields, I am trying to figure out the way to pass parameter for doc_seq_num which can be 1, 2 or 3 and soon depending on number of amendments made.

For ex: If I have some record with number 1234567890 that exists in database 4 times, once it was saved as original which gave it doc_seq_num as 1. Later record was amended 3 time now new doc_seq_num will be 4.

Now when I call stored procedure on button click, it should load the data from the database table. The record loaded would be the one with doc_seq_num= 4, not other 3.

With Below code I am able to load data into the fields with doc_seq_num 1 or whichever value passed, I am looking for help with something that will take care of any doc_seq_num.

C#
private void btnsearch_Click(object sender, EventArgs e)
{
	SqlConnection con = new SqlConnection(Data Source=serverName;Initial Catalog=camrdbd;Trusted_Connection=true);
	SqlCommand cmd = new SqlCommand();
	cmd.CommandType = CommandType.StoredProcedure;
	cmd.CommandText = "usp_CAMR_GetInfoByBatchSFN;
	con.Open();
	cmd.Connection = con;
	cmd.Parameters.AddWithValue("@sfn", txtsfn.Text);
	cmd.Parameters.AddWithValue("@BATCH_NAME", txtovrbn.Text);
	cmd.Parameters.AddWithValue("@DOC_SEQ_NUM", 1);
	cmd.Parameters.AddWithValue("@ERR_CODE", 0);
	cmd.Parameters.AddWithValue("@ERR_MS", 0);
	cmd.Parameters.AddWithValue("@TABLE_NAME", 0);
	SqlDataReader dr = cmd.ExecuteReader();
	while (dr.Read())
	{
	   txtlrn.Text = dr["lrn"].ToString();
	   txtovrbn.Text = dr["batch_name"].ToString();
	   txtovrts.Text = dr["lst_mod_dt";].ToString();
	   txtfpfn.Text = dr["first_person_fname"].ToString();
	   txtfpln.Text = dr["first_person_lnam"].ToString();
	   txtspfn.Text = dr[";second_person_fname"].ToString();
	   txtspln.Text = dr["second_person_lname"].ToString();               
	}
}

Any help and suggestions appreciated



Following is the stored procedure..

SQL
STORED PROCEDURE
@BATCH_NAME VARCHAR(12),
@SFN VARCHAR(13),
@DOC_SEQ_NUM INT,
@ERR_CODE INT OUTPUT,
@ERR_MSG VARCHAR(256) OUTPUT,
@TABLE_NAME VARCHAR(50) OUTPUT
AS

SET NOCOUNT ON

SET @ERR_CODE = 0
SET @ERR_MSG = ''
SET @TABLE_NAME = ''
SET @BATCH_NAME = UPPER(LTRIM(RTRIM(@BATCH_NAME)))
SET @SFN = LTRIM(RTRIM(@SFN))
PRINT 'BATCH_NAME = ' + @BATCH_NAME + ' AND SFN = ' + @SFN
IF LEN(@BATCH_NAME) = 12 AND LEN(@SFN) = 13
BEGIN
SELECT DISTINCT A.SFN, A.DOC_SEQ_NUM, B.LRN, A.BATCH_NAME, B.FIRST_PERSON_LNAME, B.FIRST_PERSON_FNAME, B.FIRST_PERSON_MNAME,
CONVERT(CHAR, B.FIRST_PERSON_DOB,101) AS FIRST_PERSON_DOB, B.SECOND_PERSON_LNAME, B.SECOND_PERSON_FNAME, B.SECOND_PERSON_MNAME,
CONVERT(CHAR, B.SECOND_PERSON_DOB,101) AS SECOND_PERSON_DOB, CONVERT(CHAR, B.DOM,101) AS DOM, CONVERT(CHAR, B.DOL,101) AS DOL,
B.AMENDMENT_IND, B.MARRIAGE_BC, B.EVENT_TYPE,
--B.PLACE_OF_EVENT,
(CASE B.PLACE_OF_EVENT WHEN '061' THEN '01' WHEN '61' THEN '01' WHEN '062' THEN '19' WHEN '62' THEN '19' WHEN '063' THEN '19' WHEN '63' THEN '19' ELSE B.PLACE_OF_EVENT END) AS PLACE_OF_EVENT,
B.CERT_IMAGE_ID, CONVERT(CHAR, B.REG_DT,101) AS REG_DT,
A.ACTION_TYPE, A.PROCESS_STATUS_FLG, A.IMAGE_LOCATION, B.LST_MOD_USERID, CONVERT(CHAR, B.LST_MOD_DT,101) AS LST_MOD_DT
FROM DBO.CAMR_DOCUMENT_CONTROL A
INNER JOIN DBO.CAMR_DOCUMENT_INDEX B
--LEFT OUTER JOIN DBO.CAMR_DOCUMENT_INDEX B
ON A.SFN = B.SFN
WHERE A.SFN = @SFN
AND A.DOC_SEQ_NUM = B.DOC_SEQ_NUM
AND A.DOC_SEQ_NUM = @DOC_SEQ_NUM
END
ELSE IF LEN(@BATCH_NAME) <> 12 AND LEN(@SFN) = 13
BEGIN
SELECT A.SFN, A.DOC_SEQ_NUM, B.LRN, A.BATCH_NAME, B.FIRST_PERSON_LNAME, B.FIRST_PERSON_FNAME, B.FIRST_PERSON_MNAME,
CONVERT(CHAR, B.FIRST_PERSON_DOB,101) AS FIRST_PERSON_DOB, B.SECOND_PERSON_LNAME, B.SECOND_PERSON_FNAME, B.SECOND_PERSON_MNAME,
CONVERT(CHAR, B.SECOND_PERSON_DOB,101) AS SECOND_PERSON_DOB, CONVERT(CHAR, B.DOM,101) AS DOM, CONVERT(CHAR, B.DOL,101) AS DOL,
B.AMENDMENT_IND, B.MARRIAGE_BC, B.EVENT_TYPE,
--B.PLACE_OF_EVENT,
(CASE B.PLACE_OF_EVENT WHEN '061' THEN '01' WHEN '61' THEN '01' WHEN '062' THEN '19' WHEN '62' THEN '19' WHEN '063' THEN '19' WHEN '63' THEN '19' ELSE B.PLACE_OF_EVENT END) AS PLACE_OF_EVENT,
B.CERT_IMAGE_ID, CONVERT(CHAR, B.REG_DT,101) AS REG_DT,
A.ACTION_TYPE, A.PROCESS_STATUS_FLG, A.IMAGE_LOCATION, B.LST_MOD_USERID, CONVERT(CHAR, B.LST_MOD_DT,101) AS LST_MOD_DT
FROM DBO.CAMR_DOCUMENT_CONTROL A, DBO.CAMR_DOCUMENT_INDEX B
WHERE A.SFN = B.SFN
AND A.SFN = @SFN
AND A.DOC_SEQ_NUM = B.DOC_SEQ_NUM
AND A.DOC_SEQ_NUM = @DOC_SEQ_NUM
END
ELSE IF LEN(@BATCH_NAME) = 12 AND LEN(@SFN) <> 13
BEGIN
SELECT A.SFN, A.DOC_SEQ_NUM, B.LRN, A.BATCH_NAME, B.FIRST_PERSON_LNAME, B.FIRST_PERSON_FNAME, B.FIRST_PERSON_MNAME,
CONVERT(CHAR, B.FIRST_PERSON_DOB,101) AS FIRST_PERSON_DOB, B.SECOND_PERSON_LNAME, B.SECOND_PERSON_FNAME, B.SECOND_PERSON_MNAME,
CONVERT(CHAR, B.SECOND_PERSON_DOB,101) AS SECOND_PERSON_DOB, CONVERT(CHAR, B.DOM,101) AS DOM, CONVERT(CHAR, B.DOL,101) AS DOL,
B.AMENDMENT_IND, B.MARRIAGE_BC, B.EVENT_TYPE,
--B.PLACE_OF_EVENT,
(CASE B.PLACE_OF_EVENT WHEN '061' THEN '01' WHEN '61' THEN '01' WHEN '062' THEN '19' WHEN '62' THEN '19' WHEN '063' THEN '19' WHEN '63' THEN '19' ELSE B.PLACE_OF_EVENT END) AS PLACE_OF_EVENT,
B.CERT_IMAGE_ID, CONVERT(CHAR, B.REG_DT,101) AS REG_DT,
A.ACTION_TYPE, A.PROCESS_STATUS_FLG, A.IMAGE_LOCATION, B.LST_MOD_USERID, CONVERT(CHAR, B.LST_MOD_DT,101) AS LST_MOD_DT
FROM DBO.CAMR_DOCUMENT_CONTROL A, DBO.CAMR_DOCUMENT_INDEX B
WHERE A.BATCH_NAME = @BATCH_NAME
AND A.SFN = B.SFN
AND A.DOC_SEQ_NUM = B.DOC_SEQ_NUM
--AND A.DOC_SEQ_NUM = @DOC_SEQ_NUM
END
ELSE
BEGIN
SET @ERR_CODE = 50350
SET @ERR_MSG = 'Insufficient key search. Cannot retrieve marriage information.'
END

SET NOCOUNT OFF

RETURN
Posted
Updated 4-Dec-15 17:25pm
v3
Comments
phil.o 4-Dec-15 16:49pm    
If you do not show the query you have so far, how are we supposed to help? :)
It seems to be as trivial as sorting by doc_seq_num descending and taking only the first result.
George Jonsson 4-Dec-15 23:33pm    
Of course not replying to your question.
And then also add the code in the comment instead of using 'Improve question'
George Jonsson 4-Dec-15 23:35pm    
Use the reply button if you want to respond to a question, otherwise you are the only one getting notified.
Also don't add code in the comment section, use 'Improve question' instead.
Now someone done it for you.
Arasappan 5-Dec-15 0:18am    
is that wrong I done George Jonsson..
George Jonsson 5-Dec-15 1:14am    
No, it is not wrong to help people. :-)
But the people asking questions also need to understand that it is important to be as clear as possible and to include relevant information to their question.
And also to use the proper way to add information if necessary.

1 solution

In your c# code you can do something like
C#
cmd.Parameters.AddWithValue("@DOC_SEQ_NUM", seqnum ?? DBNull.Value);
(Because null in .net does not equate to NULL in SQL. Or you can omit the parameter all together, as long as...

(...) In your Stored Procedure make sure you have that parameter as nullable
SQL
@BATCH_NAME VARCHAR(12),
 @SFN VARCHAR(13),
 @DOC_SEQ_NUM INT = NULL,
 @ERR_CODE INT OUTPUT,
 @ERR_MSG VARCHAR(256) OUTPUT,
 @TABLE_NAME VARCHAR(50) OUTPUT

Now in your WHERE you can utilise the ISNULL function to ensure you handle the case where the parameter is null (or not passed in)
AND A.DOC_SEQ_NUM = ISNULL(@DOC_SEQ_NUM, A.DOC_SEQ_NUM)

However, if the parameter is null then this change will result in more than one row being returned, so you want to precede the list of columns returned with
SQL
SELECT TOP 1 A.SFN, A.DOC_SEQ_NUM, B.LRN, ... etc

To ensure you get the most recent document sequence order each query based on the sequence
ORDER BY A.DOC_SEQ_NUM DESC

The following trivial example demonstrates what I mean
SQL
 Create Table Table1
 (
	DOC_SEQ_NUM int,
	someData varchar(20)
)
insert into Table1 values
(1,'first version'),
(2,'second version'),
(3,'third version')

This variable will mimic the input parameter
SQL
DECLARE @DOC_SEQ_NUM int = NULL

Here is the cut-down query
SQL
SELECT top 1 *
FROM Table1
WHERE DOC_SEQ_NUM = ISNULL(@DOC_SEQ_NUM, DOC_SEQ_NUM)
ORDER BY DOC_SEQ_NUM DESC

This returns
C#
3	third version

But a specific version can still be requested
SQL
SET @DOC_SEQ_NUM = 2
returns
2	second version
 
Share this answer
 
v2

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