从具有某种模式的文本文件中获取数据并填充gridview和数据库

时间:2014-03-05 12:00:40

标签: text-files

我想从一个像'!@这样的模式的文本文件中填充gridview!和'#@#',填充后我想根据coloumn的顺序插入数据库中的所有数据, 就像在我的文本文件中首先有一些数字是我的数据库中的Sr_No然后我有一些名称应该在数据库中填写我的名字coloumn ....我正在提供我已经和我尝试过的

这是我的文字档案: - * *只需将其粘贴在记事本中并取消刻录文字即可了解其实际情况。

R3073101.txt

3070004626!@!JAVEED BASHIR BURZA#@#BASHIR BURZA#@#502 BLUE BELLS FLOWER VALLEY附近#@#PAYYEDE HOTEL NAYA NAGAR#@#MIRA ROAD EAST THANE#@#401107#@#502 BLUE BELLS FLOWER VALLEY #@#PAYYEDE HOTEL NAYA NAGAR#@#MIRA ROAD EAST THANE#@#401107#@#1#@#1#@#N#@#15#@#114#@#FORD FIGO 1 4 EXI#@# 4#@#1399#@#5#@#0#@#1525#@#0#@#2#@#MAJ1XXMRJ1DU78421#@#MAJ1XXMRJ1DU78421#@#C01#@#K BLUE#@#1#@#2014 #@#二零二九年一月三十日#@#307#@#1#@#O#上@ ## @#PV#@ ## @#541137#@ ## @ ## @#1/31/2014年#@ #0#@#1399#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#1/31/2014年#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#NE#@# 1#@#2014年1月31日#@#2015年1月30日#@#71050131130100129567#@#HP>< 401107>< 2014年1月31日#@# 3070004627!@!SAJID PERVEZ#@#ALEY AHMED#@#1005/6 10th FLR MEGHDOOT A#121#@ LOKHANDWALA COMPLEX#@#ANDHERI WEST MUMBAI#@#400053#@#1005/6 10TH FLR MEGHDOOT A 121#@ #LOKHANDWALA COMPLEX#@#ANDHERI WEST MUMBAI#@#400053#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 5 P TITA at#@#4#@# 1499#@#5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL12244#@#MAJAXXMRKAEL12244#@#C01#@#D WHITE#@#1#@#2014#@#1 /二千零二十九分之三十零#@#307#@#N#@#O#上@ ## @#PV#@ ## @#952517#@ ## @ ## @#2014年1月31日#@#0#@ #1499#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#IL#@#1#@# 2014年1月31日#@#2015年1月30日#@#I24131022#@# 3070004628!@!ABRAR MALIK#@#SARDAR MALIK#@#A / 703 PANCHTANTRA APT-II#@#PANCH MARG VERSOVA#@#MUMBAI#@#400061#@#A / 703 PANCHTANTRA APT-II#@#PANCH MARG VERSOVA#@#MUMBAI#@#400061#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 5 P AMB MT#@#4#@#1499#@# 5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL15204#@#MAJAXXMRKAEL15204#@#C01#@#D WHITE#@#1#@#2014#@#1/30/209 #@#307#@#1#@#O#上@ ## @#PV#@ ## @#653670#@ ## @ ## @#2014年1月31日#@#0#@#1499#@ #0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#IL#@#1#@#1/31 / 2014#@#2015年1月30日#@#PS11225946#@#HP>< 400059>< 2014年1月31日#@# 3070004629!@ AVB AMARAN#@#BABU ANTHONY CHETTY#@#B / 203 DRONAGIRI现代USHA COLONY#@#EVERSHINE NAGAR MALAD WEST#@#MUMBAI#@#400064#@#B / 203 DRONAGIRI MODERN USHA COLONY#@# EVERSHINE NAGAR MALAD WEST#@#MUMBAI#@#400064#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 5 P AMB MT#@#4#@#1499 #@#5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL15207#@#MAJAXXMRKAEL15207#@#C01#@#M SILVER#@#1#@#2014#@#1 /二千零二十九分之三十零#@#307#@#1#@#O#上@ ## @#PV#@ ## @#653670#@ ## @ ## @#2014年1月31日#@#0#@# 1499#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#NE#@#1#@#1 / 31/2014#@#1/30/2015#@#71050131130100129516#@#HP>< 3RD FLR A-WING TRADE STAR BLDG>< 400059>< 1/31/2014#@# 3070004630!@!NAROTTAM KUMAR SHARMA#@#PARMANAND SHARMA#@#103-B ASTER TOWER FILM CITY ROAD#@#FILM CITY ROAD MALAD EAST#@​​#MUMBAI#@#400097#@#103-B ASTER TOWER FILM CITY ROAD #@ #FILM CITY ROAD MALAD EAST#@​​#MUMBAI#@#400097#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 0 P TITA MT#@#4 #@#999#@#5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL17257#@#MAJAXXMRKAEL17257#@#C01#@#CHILL#@#1#@#2014#@ #1/30/2029年#@#307#@#1#@#O#上@ ## @#PV#@ ## @#895890#@ ## @ ## @#2014年1月31日#@#0 #@#999#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#IL#@#1# @#1 /二千○十四分之三十一#@#2015年1月30日#@#I-24134004#@#HP>< 400098>< 2014年1月31日#@#

我的.aspx页面: -

 <div align="left">
        <h3>
            <b>Expense Sheet</b>
        </h3>
    </div>
     <br />
    <div class="row">
        <div class="col-md-6" align="center">
            <asp:TextBox ID="TextBox1" runat="server" placeholder="Search" CssClass="form-control" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
            <br />
        </div>
        <div class="col-md-6" align="center">
            <asp:Button ID="Button1" runat="server" Text="+New" CssClass="btn btn-primary" OnClick="Button1_Click"
                 Style="font-size: 13px !important; left: 0 !important;
                margin-left: -491px !important; margin-top: -5px !important; padding: 11px 23px !important;"
                UseSubmitBehavior="False" />

        </div>
        <asp:Button ID="Button2" runat="server" Text="Upload From Text" CssClass="btn btn-primary"  
                Style="font-size: 13px !important; left: 0 !important;
                margin-left: -491px !important; margin-top: -5px !important; padding: 11px 23px !important;"
                 UseSubmitBehavior="False" onchange="FileUpload1" OnClick="Button2_Click"  />
        <asp:FileUpload ID="FileUpload1" runat="server" OnDataBinding="Button2_Click" />

    </div>
    <br />

    <div align="center" style="overflow: auto; width: 100%; white-space: nowrap;">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"
            CssClass="table table-hover table-bordered" EmptyDataText="NO SUCH DATA FOUND"
            OnPageIndexChanging="GridView1_PageIndexChanging"  OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="True" PageSize="5"
            OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="Edit/Delete" HeaderStyle-Width="15%">
                    <ItemTemplate>
                        <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
                        <span onclick="return confirm('Are you sure want to delete?')">
                            <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
                        </span>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
                        <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
                    </EditItemTemplate>
                    <HeaderStyle Width="15%"></HeaderStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Sr_No">
                    <ItemTemplate>
                        <asp:Label ID="lbsrno" runat="server" Text="<%#bind('[Sr_No]') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customer_Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCustomer_Name" runat="server" Text='<%#bind("Customer_Name") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbCustomer_Name" runat="server" Text='<%#Eval("Customer_Name") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Chassis_No">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtChassis_No" runat="server" Text='<%#bind("Chassis_No") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbChassis_No" runat="server" Text='<%#Eval("Chassis_No") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Reg_no">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtReg_no" runat="server" Text='<%#bind("Reg_no") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbReg_no" runat="server" Text='<%#Eval("Reg_no") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Imv_Fees">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtImv_Fees" runat="server" Text='<%#bind("Imv_Fees") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbImv_Fees" runat="server" Text='<%#Eval("Imv_Fees") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Petrol_Exp">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtPetrol_Exp" runat="server" Text='<%#bind("Petrol_Exp") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbPetrol_Exp" runat="server" Text='<%#Eval("Petrol_Exp") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Driver_Exp">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDriver_Exp" runat="server" Text='<%#bind("Driver_Exp") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbDriver_Exp" runat="server" Text='<%#Eval("Driver_Exp") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Conv_Exp">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtConv_Exp" runat="server" Text='<%#bind("Conv_Exp") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbConv_Exp" runat="server" Text='<%#Eval("Conv_Exp") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="xerox">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtxerox" runat="server" Text='<%#bind("xerox") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbxerox" runat="server" Text='<%#Eval("xerox") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Misc_Exp">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtMisc_Exp" runat="server" Text='<%#bind("Misc_Exp") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbMisc_Exp" runat="server" Text='<%#Eval("Misc_Exp") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total_No">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtTotal_No" runat="server" Text='<%#bind("Total_No") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbTotal_No" runat="server" Text='<%#Eval("Total_No") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="No_5">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNo_5" runat="server" Text='<%#bind("No_5") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbNo_5" runat="server" Text='<%#Eval("No_5") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="W_V">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtW_V" runat="server" Text='<%#bind("W_V") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbW_V" runat="server" Text='<%#Eval("W_V") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SpecialNo_Fess">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtSpecialNo_Fess" runat="server" Text='<%#bind("SpecialNo_Fess") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbSpecialNo_Fess" runat="server" Text='<%#Eval("SpecialNo_Fess") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Spl_No_S_c">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtSpl_No_S_c" runat="server" Text='<%#bind("Spl_No_S_c") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbSpl_No_S_c" runat="server" Text='<%#Eval("Spl_No_S_c") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Redium">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtRedium" runat="server" Text='<%#bind("Redium") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbRedium" runat="server" Text='<%#Eval("Redium") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SocFees">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtSocFees" runat="server" Text='<%#bind("SocFees") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbSocFees" runat="server" Text='<%#Eval("SocFees") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Cf">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCf" runat="server" Text='<%#bind("Cf") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbCf" runat="server" Text='<%#Eval("Cf") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Int">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtInt" runat="server" Text='<%#bind("Int") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbInt" runat="server" Text='<%#Eval("Int") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Tax">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtTax" runat="server" Text='<%#bind("Tax") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbTax" runat="server" Text='<%#Eval("Tax") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Soc_S_C">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtSoc_S_C" runat="server" Text='<%#bind("Soc_S_C") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbSoc_S_C" runat="server" Text='<%#Eval("Soc_S_C") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>

我尝试的代码是填充gridview并将其插入数据库但不是根据我的coloumns名称,这是我的代码: -

protected void Button2_Click(object sender,EventArgs e) {

DataTable table = new DataTable();
table.Columns.Add("Sr_No");
table.Columns.Add("Customer_Name");
table.Columns.Add("Chassis_No");
table.Columns.Add("Reg_no");
table.Columns.Add("Imv_Fees");
table.Columns.Add("Petrol_Exp");
table.Columns.Add("Driver_Exp");
table.Columns.Add("Conv_Exp");
table.Columns.Add("xerox");
table.Columns.Add("Misc_Exp");
table.Columns.Add("Total_No");
table.Columns.Add("No_5");
table.Columns.Add("W_V");
table.Columns.Add("SpecialNo_Fess");
table.Columns.Add("Spl_No_S_c");
table.Columns.Add("Redium");
table.Columns.Add("SocFees");
table.Columns.Add("Cf");
table.Columns.Add("Int");
table.Columns.Add("Tax");
table.Columns.Add("Soc_S_C");

    if (FileUpload1.HasFile)
    {
    string filename = Path.GetFileName(FileUpload1.FileName);
    using (StreamReader sr = new StreamReader(FileUpload1.FileContent))
        {
         while (!sr.EndOfStream)
            {
                string[] parts = sr.ReadLine().Split('!', '@', '#');
          table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7], parts[8], parts[9], parts[10], parts[11], parts[12], parts[13], parts[14], parts[15], parts[16], parts[17], parts[18], parts[19], parts[20]);

            SqlConnection con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con4.Open();
            SqlCommand cmd4 = new SqlCommand("insert into expenseSheet values('" + parts[0] + "','" + parts[1] + "','" + parts[2] + "','" + parts[3] + "','" + parts[4] + "','" + parts[5] + "','" + parts[6] + "','" + parts[7] + "','" + parts[8] + "','" + parts[9] + "','" + parts[10] + "','" + parts[11] + "','" + parts[12] + "','" + parts[13] + "','" + parts[14] + "','" + parts[15] + "','" + parts[16] + "','" + parts[17] + "','" + parts[18] + "','" + parts[19] + "','" + parts[20] + "')", con4);

            SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
            da4.Fill(table);

            GridView1.DataSource = table;
            GridView1.DataBind();
               }
        }
    }

}

就像点击我的按钮2一样,它应该填充我的网格和数据库

提前致谢.......

0 个答案:

没有答案