5

Sorry about the code earlier. Could anyone please help? I have a gridview GridView1 which is populated on PageLoad() by importing from Excel sheet that has three columns:

  1. Date
  2. Customers
  3. PayingBookNoOrDD

The sheet has five rows. Users can edit the data in textboxes on the page (markup below). After editing, when the user clicks on the submit button, I need to get these new values from all the textboxes and update the same Excel sheet from where the GridView was populated.

I created three string arrays: dateArray, custArray, and payingInBookArray to store these new values. But when I run the application all three arrays are empty.

Markup:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Date,Customers,PayingInBookNoOrDD" >
    <Columns>
    <asp:TemplateField>
        <HeaderTemplate>Date</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtDate" Text='<%# Bind("Date") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <HeaderTemplate>Customers</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtCustomers" Text='<%# Bind("Customers") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <HeaderTemplate>PayingInBookNoOrDD</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtPayingInBookNoOrDD" Text='<%# Bind("PayingInBookNoOrDD") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:Button ID="txtSubmit" runat="server" Text="Submit" onclick="txtSubmit_Click" />

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    string selectQuery = "SELECT * FROM [Month1$B2:D5]";
    OleDbConnection conn = new OleDbConnection(connString);

    conn.Open();

    OleDbDataAdapter da = new OleDbDataAdapter(selectQuery, conn);
    DataSet ds = new DataSet();
    da.Fill(ds);

    GridView1.DataSource = ds;
    GridView1.DataBind();

    conn.Close();
    da.Dispose();
    conn.Dispose();
}

protected void txtSubmit_Click(object sender, EventArgs e)
{
    IList<string> DateArray = new List<string>();
    IList<string> custArray = new List<string>();
    IList<string> payInBookArray = new List<string>();

    foreach (GridViewRow gr in GridView1.Rows)
    {
        TextBox lblDate = (TextBox)gr.Cells[0].FindControl("txtDate");
        DateArray.Add(lblDate.Text);

        TextBox lblCustomers = (TextBox)gr.Cells[1].FindControl("txtCustomers");
        custArray.Add(lblCustomers.Text);

        TextBox lblPayInBookNo = (TextBox)gr.Cells[2].FindControl("txtPayingInBookNoOrDD");
        payInBookArray.Add(lblPayInBookNo.Text);
    }

    ExportToExcel(DateArray.ToArray(), custArray.ToArray(), payInBookArray.ToArray()); 

}

Please let me know if anyone has a solution.

Thanks.

jwheron
  • 2,553
  • 2
  • 30
  • 40
Amol
  • 127
  • 1
  • 7
  • Please clean-up your code examples...way too much to edit – CAbbott Oct 06 '11 at 18:00
  • Hi CAbbott, Sorry I am new to this site. I tried to edit the question but it got even more messier. Please give me a few minutes and I will fix it. – Amol Oct 06 '11 at 18:16
  • Dont call your buttons `txtSubmit` or anything prefacing a `txt` if its really a button `btn`, it can be very misleading. You also should wrap your connection object using the `using` statement, that way you don't have to call `.dispose()` explicitly. – JonH Oct 06 '11 at 18:59

4 Answers4

0
Protected Sub txtNombres_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("FiltroNombres") = DirectCast(sender, TextBox).Text

End Sub
mhlester
  • 22,781
  • 10
  • 52
  • 75
wil
  • 1
0

try using the following method to collect the value

foreach (GridViewRow gr in GridView1.Rows)

{

string date= ((TextBox)gr.FindControl("txtDate")).text;

}

0

Add a postback check on your Page_Load event. I can't see anything wrong with your btn_Submit code.

protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack){
         string selectQuery = "SELECT * FROM [Month1$B2:D5]";
         OleDbConnection conn = new OleDbConnection(connString);
         conn.Open();
         OleDbDataAdapter da = new OleDbDataAdapter(selectQuery, conn);
         DataSet ds = new DataSet();
         da.Fill(ds);
         GridView1.DataSource = ds;
         GridView1.DataBind();
         conn.Close();
         da.Dispose();
         conn.Dispose();
    }
}
Gilbert
  • 36
  • 2
0

Personally, I would change your txtSubmit_Click function to this:

protected void txtSubmit_Click(object sender, EventArgs e)
{
    IList<string> DateArray = new List<string>();
    IList<string> custArray = new List<string>();
    IList<string> payInBookArray = new List<string>();

    foreach (GridViewRow gr in GridView1.Rows)
    {
        TextBox lblDate = (TextBox)gr.FindControl("txtDate");
        DateArray.Add(lblDate.Text);

        TextBox lblCustomers = (TextBox)gr.FindControl("txtCustomers");
        custArray.Add(lblCustomers.Text);

        TextBox lblPayInBookNo = (TextBox)gr.FindControl("txtPayingInBookNoOrDD");
        payInBookArray.Add(lblPayInBookNo.Text);
    }

    ExportToExcel(DateArray.ToArray(), custArray.ToArray(), payInBookArray.ToArray()); 

}

I've always had problems trying to directly access values in the .Cells collection. What happens when you call .FindControl on the row itself?

As others have said, it's worth thinking about new names for your HTML fields and your variables. It seems trivial now to have DateArray of type IList, but it briefly threw me for a loop to see DateArray.ToArray(). Naming conventions and other small source code changes won't take you a lot of time to fix now, but will save you plenty of time later when you have to revisit this code after weeks or months of working on other projects.

jwheron
  • 2,553
  • 2
  • 30
  • 40