I'm hoping I'm missing something good here because this problem is driving me crazy. Here is my what my little web page does.
1. Imports a ^ delimited text file
2. Sorts through the data and places it into a datatable
3. Bind DataTable to the GridView so I can do some data entry at that point
4. Read a couple values from the GridView then perform database inserts.
Here is my code:
text file being imported
Is the postback wiping out my gridview values? If so, how do I prevent that? I cant think of anything else happening.
1. Imports a ^ delimited text file
2. Sorts through the data and places it into a datatable
3. Bind DataTable to the GridView so I can do some data entry at that point
4. Read a couple values from the GridView then perform database inserts.
Here is my code:
Code:
public partial class _Default : System.Web.UI.Page
{
protected static string strFileName = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//if (strFileName != String.Empty)
//{
// GenerateRecords();
//}
}
}
protected void btnImport_Click(object sender, EventArgs e)
{
GenerateRecords();
}
protected void GenerateRecords()
{
if (strFileName == String.Empty)
{
strFileName = FileUpload1.PostedFile.FileName;
}
StreamReader sr;
sr = File.OpenText(strFileName);
string strContents = String.Empty;
char strDelimiter = '^';
DataTable dtHotels = CreateMappingTable();
while (sr.Read() > 0)
{
strContents = sr.ReadLine();
string[] strSplit = strContents.Split(strDelimiter);
DataRow dr = dtHotels.NewRow();
dr["CustomerHotelID"] = strSplit[0];
dr["PropertyInfo"] = strSplit[1] + " " + strSplit[2] + " " + strSplit[3] + " " + strSplit[4] + " " + strSplit[5];
dtHotels.Rows.Add(dr);
}
sr.Close();
grdvHotels.DataSource = dtHotels;
grdvHotels.DataBind();
}
DataTable CreateMappingTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("CustomerHotelID");
dt.Columns.Add("PropertyInfo");
return dt;
}
protected void btnInsert_Click(object sender, EventArgs e)
{
string strCustomerID = ddlCustomers.SelectedValue;
foreach(GridViewRow dr in grdvHotels.Rows)
{
TextBox tb = (TextBox)dr.FindControl("txtHoteldID");
}
}
protected bool InsertHotelMappings()
{
//database code here
return true;
}
}
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" /><br />
<br />
<asp:DropDownList ID="ddlCustomers" runat="server" DataSourceID="SqlDataSource1"
DataTextField="CustomerName" DataValueField="API_CustomerID">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TripresConnectionString %>"
SelectCommand="SELECT [API_CustomerID], [CustomerName] FROM [API_Customers] ORDER BY [CustomerName]">
</asp:SqlDataSource>
<asp:GridView ID="grdvHotels" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Customer ID">
<ItemTemplate>
<asp:Label ID="lblCustHotelID" runat="server"><%# Eval("CustomerHotelID") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Property Info">
<ItemTemplate>
<asp:Label ID="lblPropertyInfo" runat="server"><%# Eval("PropertyInfo") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TripRes Hotel ID">
<ItemTemplate>
<asp:TextBox ID="txtHoteldID" runat="server" Width="50"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnInsert" runat="server" Text="Insert Mappings" OnClick="btnInsert_Click" /></div>
</form>
</body>
</html>
text file being imported
Code:
^FLAM^Flaming-o Hotel and Casino^3555 Las Vegas Blvd.^Las Vegas^NV^89109
^LUX1^Luxor Hotel and Casino^3900 Las Vegas Blvd.^Las Vegas^NV^89119
^MAND35^Mandalay Bay Hotel and Casino^3950 Las Vegas Boulevard South^Las Vegas^NV^89119
Is the postback wiping out my gridview values? If so, how do I prevent that? I cant think of anything else happening.