Click here to Skip to main content
16,022,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am taking some values from database and then i Want split that values and then
find highest number from that values

What I have tried:

I have function
1)
<pre> public DataSet AutogenerateServiceno()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Proc_autogeneratesvno1", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
           
        }

it gives me values in ds as
sv_10
sv_11
sv_12
sv_4
sv_9


2) I am calling above function from another function as bellow

 public string AutogenerateSvno()
        {
            string s2 = "sv_";
            DataSet ds = objdal.AutogenerateServiceno(); //  here values in s=sv_10, sv_11, sv_12, sv_4, sv_9.
// now I want seperate Sv and number seperate and want to insert number in one // //arrar and number values in another array i.e values 10,11,12,4,9 in one array.
// then I want to find Highest Number from numeric array and in that number //increment by one and then concate that incremented number to sv_(number) and //return that values to presentation layer  for that I have written following code.
//but it giving exception at  
                arrval1[i] =Convert.ToString(arrvalues[i].Split('_'));
           
 string[] arrvalues = new string[ds.Tables[0].Rows.Count];
            string[] arrval1 = new string[ds.Tables[0].Rows.Count];
            int[] arrval = new int[ds.Tables[0].Rows.Count];

            //loopcounter
            for (int loopcounter = 0; loopcounter < ds.Tables[0].Rows.Count; loopcounter++)
            {
                string str;
               // .Split('_');
                //assign dataset values to array
                arrvalues[loopcounter] =ds.Tables[0].Rows[loopcounter]["svno"].ToString();
            }
            for (int i = 0; i < arrval.Length - 1; i++)
            {
                arrval1[i] =Convert.ToString(arrvalues[i].Split('_'));  // Error Here
                arrval[i] = int.Parse(arrval1[i]);
            }
            //string[] s1 = s.Split('_');
            //int i = int.Parse(s1[1]);
            //i++;
            //s2 = s2 + i
            return s2;
        }
Posted
Updated 4-Sep-17 15:25pm
Comments
PIEBALDconsult 4-Sep-17 14:48pm    
Of course you have an error there. I'm guessing you might want to index into the array that Split returns.
In any case, you do not need to use Convert.ToString .

We can't tell - too much depends on your data (which we don't have access to) and other factors while the code is running (which we can't do, because we don't have access to your data).
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
I'm guessing that this is what you are looking for:
C#
var list = new List<string>()
    { "sv_10", "sv_11", "sv_12", "sv_4", "sv_9" };

var best = list.OrderByDescending(x => int.Parse(x.Split(new[] { '_' }).Last()))
                .First();
 
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