Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btn_submit(object sender, EventArgs e)
    {
        string data = "";
        foreach (GridViewRow row in GrdRole.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
                if (chkRow.Checked)
                {
                    string EmployeeNo = row.Cells[2].Text;
                    data = data + EmployeeNo + ",";


                }
            }
        }
    }


any idea how to separate this?my checkbox output = 30100001,30100002,30100004,30100005,
i wan insert it one by one to my database

What I have tried:

C#
string[] data1 = data.Split(',');
        for (int i = 0; i < data.Length; i++)
        {
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + data1 + "');", true);
        }
Posted
Updated 4-Jul-16 16:46pm
v2
Comments
Garth J Lancaster 4-Jul-16 21:07pm    
I dont see the issue with

string[] data1 = data.Split(',');

if data is "301000001,301000002" then data1 will be

data1[0] = "301000001"
data1[1] = "301000002"
...

the only issue I see here

for (int i = 0; i < data.Length; i++)
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + data1 + "');", true);
}

is that data1 is an array, isnt it ? so dont you need

for (int i = 0; i < data.Length; i++)
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + data1[i] + "');", true);
}
Beginner Luck 4-Jul-16 21:15pm    
use linq

1 solution

I see a few problems in your code:
C#
string[] data1 = data.Split(',');
for (int i = 0; i < data1.Length; i++)
{
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + data1[i] + "');", true);
}

See corrections.

Why are you doing things in such a complicated way ?
1) you got a datagrid holding EmployeeNo
2) you concatenate the EmployeeNo in a string
3) you split the string in a list if EmployeeNo
4) you launch a database script with each EmployeeNo

You can merge 2 and 3 by building the list directly and skipping the string step.
you can simplify further by not using the list either and launch the script as you know the EmployeeNo:
C#
if (chkRow.Checked)
{
    string EmployeeNo = row.Cells[2].Text;
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + EmployeeNo + "');", true);
}
 
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