Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i code to row has content and i have to split it into sentence using button in gridview and store into table so i have written this code.but its not working

x.aspx:

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"  Width="219px"  OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand1">
            <Columns>
                <asp:BoundField DataField="ContentText" HeaderText="ContentText" SortExpression="ContentText" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btn1" Text="Split into Sentences"  runat="server" CommandName="click" CommandArgument="<%# Container.DisplayIndex %>"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Jobe101ConnectionString %>" SelectCommand="SELECT [ContentText] FROM [Contents]"></asp:SqlDataSource>




x.aspx.cs
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e )
{


if (e.CommandName == "click")
{

int index = Convert.ToInt32(e.CommandArgument.ToString());

GridViewRow row = GridView1.Rows[index];

var text = GridView1.Rows[index].Cells[0].Text;

String[] sentences = Regex.Split(text, @"(?<=[.!?])\s+(?=\p{Lt})");

System.Data.DataTable dt1 = new System.Data.DataTable();

dt1.Columns.Add("SentenceText");

foreach (string value in sentences)
{
dt1.Rows.Add(value);
}
string sql = "";
string apply = "";
for (int i = 0; i < dt1.Rows.Count; i++)
{
sql = sql + "insert into Sentences(SentenceText) select '" + dt1.Rows[i]["SentenceText"].ToString() + "' where not exists(select SentenceText from Sentences where='" + dt1.Rows[i]["SentenceText"].ToString() + "')";
apply = apply + "insert into ContentSentences(ContentID)select ContentID from Contents";
}



}

what is problem???/
Posted
Updated 26-Feb-15 19:19pm
v2

1 solution

You have to manage also the case when RowIndex of the grid is -1. In you case you should add the next code before to access the row cell.
C#
if(index <0)
{
   return;
}
//
GridViewRow row = GridView1.Rows[index];
//...
 
Share this answer
 
Comments
vatsaldesai 27-Feb-15 1:26am    
ok but my content are not completely splits up only few lines are splits. is it problem with store in datatable.
Raul Iloc 27-Feb-15 8:20am    
You should manage this special case when GridView1_RowCommand1 event is generated with index -1 and this is nor related with split.

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