yogi affandi

yogi affandi

  • NA
  • 38
  • 595

Hello friend, i create simple program but not in database

Dec 9 2017 1:12 PM
Hello friend,
 
i create simple program but not in database and i check in Visual studio no Error, is there anybody can help me?
 
I am using Visual Studio 2017
 
Thanks
 
Yogi Affandi
 
webForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="yogi.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>YOGI AFFANDI </title>
</head>
<body>
<form id="form1" runat="server">
<div style="color: red;">
<h4>
</h4>
<table>
<tr>
<td>
Select File
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Upload" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
 
WebForm1.aspx.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
namespace yogi
{
public partial class WebForm1 : System.Web.UI.Page
{
OleDbConnection Econ;
SqlConnection con;
string constr, Query, sqlconn;
protected void Page_Load(object sender, EventArgs e)
{
}
private void ExcelConn(string FilePath)
{
constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath);
Econ = new OleDbConnection(constr);
}
private void connection()
{
sqlconn = ConfigurationManager.ConnectionStrings["SqlCom"].ConnectionString;
con = new SqlConnection(sqlconn);
}
private void InsertExcelRecords(string FilePath)
{
ExcelConn(FilePath);
Query = string.Format("Select [CARD_NBR],[AMOUNT],[NO] FROM [{0}]", "Sheet1$");
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
Econ.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable Exceldt = ds.Tables[0];
connection();
//creating object of SqlBulkCopy
SqlBulkCopy objbulk = new SqlBulkCopy(con);
//assigning Destination table name
objbulk.DestinationTableName = "Table400";
//Mapping Table column
objbulk.ColumnMappings.Add("CARD_NBR", "CARD_NBR");
objbulk.ColumnMappings.Add("AMOUNT", "AMOUNT");
objbulk.ColumnMappings.Add("NO", "NO");
//inserting Datatable Records to DataBase
con.Open();
objbulk.WriteToServer(Exceldt);
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
InsertExcelRecords(CurrentFilePath);
}
}
}
 
web.config
 
<connectionStrings>
<add name="SqlCom" connectionString="Data Source=LAPTOP-SSSKFT0\SQLEXPRESS;Initial Catalog=AS400;User Id=sa;Password=Password1!" providerName="System.Data.SqlClient" />
</connectionStrings>

Answers (1)