Click here to Skip to main content
15,031,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm pulling data into my datagridview table via sql. I want to generate more than the ID number in any row I choose and use it when creating a new record. Normally I can produce more of the last record I chose, but when I make a selection in any row, I want the highest value according to the ID number there. Hope I was able to explain my problem.

What I have tried:

i have tried this code

im using this code below the lines.

C#
public void Code_Create()
        {
            Valuedetails u = new Valuedetails();
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT max(VALUEID) from VALUES_1 WHERE VALUEID=@VALUEID", conn);
            comm.Parameters.Add("@VALUEID", SqlDbType.VarChar).Value = Variables.valueID;
            u.ValueID = Convert.ToString(comm.ExecuteScalar());
            conn.Close();
            int kod = int.Parse(Regex.Replace(u.ValueID, "[^0-9]", "")) + 1;
            string zeroStr = Regex.Replace(u.ValueID, "[^0-9]", "");
            string newZeroStr = "";
            for (int x = 0; x < zeroStr.Length; x++)
                if (zeroStr[x] == '0') newZeroStr = newZeroStr + "0";
                else { };
            string newVal = Regex.Replace(u.ValueID, "[0-9]", "") + newZeroStr + kod;
            txtvalueID.Text = newVal;            
        }


Program's view;

Code  |   Details
0001  | Pencil
0002  | Paper
0003  | Rubber
tk22  | Battery
tk23  | Glass
................


if i select 002 | Paper, my code has to give new ID code 0004, or select tk22 | Battery has to give me new record code tk24
Posted
Updated 12-Jan-21 2:16am
v2
Comments
Gerry Schmitz 12-Jan-21 9:38am
   
Faulty business logic. Keys should be "nonsense"; you're building a business around one.

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