how to import excel file data in sql table with not null column only with stored procedure while importing excel fileAdd a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?Insert results of a stored procedure into a temporary tableHow can I get column names from a table in SQL Server?Function vs. Stored Procedure in SQL ServerFind all tables containing column with specified name - MS SQL ServerSearch text in stored procedure in SQL ServerHow do I import an SQL file using the command line in MySQL?

Palatino font (newpxmath) misaligns text in fraction numerators

Is the mass of paint relevant in rocket design?

Why weren't the Death Star plans transmitted electronically?

What's the next step in this Unequal (Futoshiki) puzzle?

To what extent is it worthwhile to report check fraud / refund scams?

Why does this image of Jupiter look so strange?

Safe to use 220V electric clothes dryer when building has been bridged down to 110V?

Does wetting a beer glass change the foam characteristics?

Is it possible to encode a message in such a way that can only be read by someone or something capable of seeing into the very near future?

What's the story to "WotC gave up on fixing Polymorph"?

Is it true that, "just ten trading days represent 63 per cent of the returns of the past 50 years"?

How to justify a team increase when the team is doing good?

Can a broken/split chain be reassembled?

Subverting the emotional woman and stoic man trope

Can I take NEW (still in their boxes) PC PARTS in my checked in luggage?

What exactly did this mechanic sabotage on the American Airlines 737, and how dangerous was it?

Why does NASA publish all the results/data it gets?

I reverse the source code, you negate the input!

Can an integer optimization problem be convex?

How do I deal with too many NPCs in my campaign?

Going to France with limited French for a day

Is this Portent-like spell balanced?

Hilbert's hotel: why can't I repeat it infinitely many times?

Why are there two fundamental laws of logic?



how to import excel file data in sql table with not null column only with stored procedure while importing excel file


Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?Insert results of a stored procedure into a temporary tableHow can I get column names from a table in SQL Server?Function vs. Stored Procedure in SQL ServerFind all tables containing column with specified name - MS SQL ServerSearch text in stored procedure in SQL ServerHow do I import an SQL file using the command line in MySQL?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















working with sql and .net, i had a situation to import bulk data with excel file and huge amount of data.



The table had also 20+ columns some of then are not mandatory.



the table structure is like below :



CREATE TABLE [dbo].[Employee] (
[EmployeeID] INT IDENTITY (1, 1) NOT NULL,
[EmpID] NVARCHAR (10) NULL,
[isActive] BIT DEFAULT ((0)) NOT NULL,
[Fname] NVARCHAR (50) NOT NULL,
[Mname] NVARCHAR (10) NULL,
[Lname] NVARCHAR (50) NULL,
[DOB] DATE NOT NULL,
[DOJ] DATE NOT NULL,
[gender] NVARCHAR (10) NOT NULL,
[M_Status] NVARCHAR (10) NULL,
[Father_Name] NVARCHAR (50) NULL,
[Mother_Name] NVARCHAR (50) NULL,
[Address1] NVARCHAR (20) NULL,
[Address2] NVARCHAR (20) NULL,
[Address3] NVARCHAR (20) NULL,
[Pin_Code] NUMERIC (6) NULL,
[StateID] INT NULL,
[DistrictID] INT NULL,
[Mobile1] NUMERIC (10) NOT NULL,
[mobile2] NUMERIC (10) NULL,
[email_address] NVARCHAR (50) NULL,
[IFSC_Code] NCHAR (11) NULL,
[bank_account] NCHAR (18) NULL,
[parmanent_account] NVARCHAR (10) NULL,
[aadhar_number] NVARCHAR (12) NOT NULL,
[ESIC] NVARCHAR (10) NULL,
[UAN] NVARCHAR (10) NULL,
[SalaryID] INT NOT NULL,
[DivisionID] INT NOT NULL,
[BranchID] INT NOT NULL,
[last_used] INT NOT NULL,
[access_time] DATETIME NOT NULL,
PRIMARY KEY CLUSTERED ([EmployeeID] ASC));


after that i like to import only not null fields and display the Excel data into gridview and then upload the data to table.



my gridview code is look like :



<div class="container blue">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="myGridClass" >
<Columns>
<asp:BoundField DataField="Emp_ID" HeaderText="Employee ID" />
<asp:BoundField DataField="First_name" HeaderText="First Name" />
<asp:BoundField DataField="Last_Name" HeaderText="Last Name" />
<asp:BoundField DataField="Gender" HeaderText="Male/Female" />
<asp:BoundField DataField="Date_Birth" HeaderText="Date of Birth" DataFormatString="0:dd/MM/yyyy" />
<asp:BoundField DataField="Father_Name" HeaderText="Father Name" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile No." />
<asp:BoundField DataField="AADHAR_Number" HeaderText="AADHAR No." />
<asp:BoundField DataField="Date_Joining" HeaderText="Date of Joining" DataFormatString="0:dd/MM/yyyy" />
<asp:BoundField DataField="UAN" HeaderText="UAN" />
<asp:BoundField DataField="ESIC" HeaderText="ESIC" />
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Salary">
<EditItemTemplate>
<asp:DynamicControl ID="DynamicControl1" runat="server" DataField="SalaryID" Mode="Edit" />
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="Salary_Select" runat="server" CssClass="dropbox" DataSourceID="Salary_List" DataTextField="Post_Name" DataValueField="SalaryID">
</asp:DropDownList>
<asp:SqlDataSource ID="Salary_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Post_Name], [SalaryID] FROM [Salary]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Division">
<EditItemTemplate>
<asp:DynamicControl ID="DynamicControl1" runat="server" DataField="DivisionID" Mode="Edit" />
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="Division_Select" runat="server" CssClass="dropbox" DataSourceID="Division_List" DataTextField="Division_Name" DataValueField="DivisionID" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="Division_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Division_Name], [Company_ID], [DivisionID] FROM [Division]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Branch">
<EditItemTemplate>
<asp:DynamicControl ID="DynamicControl1" runat="server" DataField="BranchID" Mode="Edit" />
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="Branch_Select" runat="server" CssClass="dropbox" DataSourceID="branch_List" DataTextField="Branch_Name" DataValueField="BranchID">
</asp:DropDownList>
<asp:SqlDataSource ID="branch_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [BranchID], [CompanyID], [DivisionID], [Branch_Name] FROM [Branch] WHERE ([DivisionID] = @DivisionID)">
<SelectParameters>
<asp:ControlParameter ControlID="Division_Select" Name="DivisionID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="salary_source" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [EmployeeID], [Fname], [Mname], [Lname], [DOB], [gender], [Father_Name], [Mobile1], [email_address], [IFSC_Code], [bank_account], [parmanent_account], [aadhar_number], [ESIC], [UAN], [EmpID], [Mother_Name], [DistrictID], [M_Status] FROM [Employee]"></asp:SqlDataSource>
</div>


now the import code come into effect.



Public Sub Import_Employees(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String, gdview As GridView)
Dim conStr As String = ""
Select Case Extension
Case ".xls"
'Excel 97-03
conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
Exit Select
Case ".xlsx"
'Excel 07
conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
Exit Select
End Select
conStr = String.Format(conStr, FilePath, isHDR)
Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dt As New DataTable()
cmdExcel.Connection = connExcel
'Get the name of First Sheet
connExcel.Open()
Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
connExcel.Close()
'Read Data from First Sheet
connExcel.Open()
cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
oda.SelectCommand = cmdExcel
oda.Fill(dt)
connExcel.Close()
'Bind Data to GridView
'GridView1.Caption = Path.GetFileName(FilePath)
gdview.DataSource = dt
gdview.DataBind()
End Sub


function is called on page like below :



If FileUpload1.HasFile Then
Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
Dim FilePath As String = Server.MapPath(FolderPath + FileName)
FileUpload1.SaveAs(FilePath)
cls.Import_Employees(FilePath, Extension, 0, GridView1)
Upload.Visible = True
'Import_To_Grid(FilePath, Extension, 0)
End If


I tried some of link below not work for me.



https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-using-Stored-Procedure-in-ASPNet.aspx



https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-in-ASPNet-using-C-and-VBNet.aspx



https://www.aspsnippets.com/Articles/Insert-Save-Multiple-rows-from-DataGridView-to-Database-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx



Basically I am trying to import not null table data into table with excel file.










share|improve this question






























    0















    working with sql and .net, i had a situation to import bulk data with excel file and huge amount of data.



    The table had also 20+ columns some of then are not mandatory.



    the table structure is like below :



    CREATE TABLE [dbo].[Employee] (
    [EmployeeID] INT IDENTITY (1, 1) NOT NULL,
    [EmpID] NVARCHAR (10) NULL,
    [isActive] BIT DEFAULT ((0)) NOT NULL,
    [Fname] NVARCHAR (50) NOT NULL,
    [Mname] NVARCHAR (10) NULL,
    [Lname] NVARCHAR (50) NULL,
    [DOB] DATE NOT NULL,
    [DOJ] DATE NOT NULL,
    [gender] NVARCHAR (10) NOT NULL,
    [M_Status] NVARCHAR (10) NULL,
    [Father_Name] NVARCHAR (50) NULL,
    [Mother_Name] NVARCHAR (50) NULL,
    [Address1] NVARCHAR (20) NULL,
    [Address2] NVARCHAR (20) NULL,
    [Address3] NVARCHAR (20) NULL,
    [Pin_Code] NUMERIC (6) NULL,
    [StateID] INT NULL,
    [DistrictID] INT NULL,
    [Mobile1] NUMERIC (10) NOT NULL,
    [mobile2] NUMERIC (10) NULL,
    [email_address] NVARCHAR (50) NULL,
    [IFSC_Code] NCHAR (11) NULL,
    [bank_account] NCHAR (18) NULL,
    [parmanent_account] NVARCHAR (10) NULL,
    [aadhar_number] NVARCHAR (12) NOT NULL,
    [ESIC] NVARCHAR (10) NULL,
    [UAN] NVARCHAR (10) NULL,
    [SalaryID] INT NOT NULL,
    [DivisionID] INT NOT NULL,
    [BranchID] INT NOT NULL,
    [last_used] INT NOT NULL,
    [access_time] DATETIME NOT NULL,
    PRIMARY KEY CLUSTERED ([EmployeeID] ASC));


    after that i like to import only not null fields and display the Excel data into gridview and then upload the data to table.



    my gridview code is look like :



    <div class="container blue">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="myGridClass" >
    <Columns>
    <asp:BoundField DataField="Emp_ID" HeaderText="Employee ID" />
    <asp:BoundField DataField="First_name" HeaderText="First Name" />
    <asp:BoundField DataField="Last_Name" HeaderText="Last Name" />
    <asp:BoundField DataField="Gender" HeaderText="Male/Female" />
    <asp:BoundField DataField="Date_Birth" HeaderText="Date of Birth" DataFormatString="0:dd/MM/yyyy" />
    <asp:BoundField DataField="Father_Name" HeaderText="Father Name" />
    <asp:BoundField DataField="Mobile" HeaderText="Mobile No." />
    <asp:BoundField DataField="AADHAR_Number" HeaderText="AADHAR No." />
    <asp:BoundField DataField="Date_Joining" HeaderText="Date of Joining" DataFormatString="0:dd/MM/yyyy" />
    <asp:BoundField DataField="UAN" HeaderText="UAN" />
    <asp:BoundField DataField="ESIC" HeaderText="ESIC" />
    <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Salary">
    <EditItemTemplate>
    <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="SalaryID" Mode="Edit" />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:DropDownList ID="Salary_Select" runat="server" CssClass="dropbox" DataSourceID="Salary_List" DataTextField="Post_Name" DataValueField="SalaryID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="Salary_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Post_Name], [SalaryID] FROM [Salary]"></asp:SqlDataSource>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Division">
    <EditItemTemplate>
    <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="DivisionID" Mode="Edit" />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:DropDownList ID="Division_Select" runat="server" CssClass="dropbox" DataSourceID="Division_List" DataTextField="Division_Name" DataValueField="DivisionID" AutoPostBack="True">
    </asp:DropDownList>
    <asp:SqlDataSource ID="Division_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Division_Name], [Company_ID], [DivisionID] FROM [Division]"></asp:SqlDataSource>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Branch">
    <EditItemTemplate>
    <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="BranchID" Mode="Edit" />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:DropDownList ID="Branch_Select" runat="server" CssClass="dropbox" DataSourceID="branch_List" DataTextField="Branch_Name" DataValueField="BranchID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="branch_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [BranchID], [CompanyID], [DivisionID], [Branch_Name] FROM [Branch] WHERE ([DivisionID] = @DivisionID)">
    <SelectParameters>
    <asp:ControlParameter ControlID="Division_Select" Name="DivisionID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="salary_source" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [EmployeeID], [Fname], [Mname], [Lname], [DOB], [gender], [Father_Name], [Mobile1], [email_address], [IFSC_Code], [bank_account], [parmanent_account], [aadhar_number], [ESIC], [UAN], [EmpID], [Mother_Name], [DistrictID], [M_Status] FROM [Employee]"></asp:SqlDataSource>
    </div>


    now the import code come into effect.



    Public Sub Import_Employees(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String, gdview As GridView)
    Dim conStr As String = ""
    Select Case Extension
    Case ".xls"
    'Excel 97-03
    conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
    Exit Select
    Case ".xlsx"
    'Excel 07
    conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
    Exit Select
    End Select
    conStr = String.Format(conStr, FilePath, isHDR)
    Dim connExcel As New OleDbConnection(conStr)
    Dim cmdExcel As New OleDbCommand()
    Dim oda As New OleDbDataAdapter()
    Dim dt As New DataTable()
    cmdExcel.Connection = connExcel
    'Get the name of First Sheet
    connExcel.Open()
    Dim dtExcelSchema As DataTable
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
    connExcel.Close()
    'Read Data from First Sheet
    connExcel.Open()
    cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
    oda.SelectCommand = cmdExcel
    oda.Fill(dt)
    connExcel.Close()
    'Bind Data to GridView
    'GridView1.Caption = Path.GetFileName(FilePath)
    gdview.DataSource = dt
    gdview.DataBind()
    End Sub


    function is called on page like below :



    If FileUpload1.HasFile Then
    Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
    Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
    Dim FilePath As String = Server.MapPath(FolderPath + FileName)
    FileUpload1.SaveAs(FilePath)
    cls.Import_Employees(FilePath, Extension, 0, GridView1)
    Upload.Visible = True
    'Import_To_Grid(FilePath, Extension, 0)
    End If


    I tried some of link below not work for me.



    https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-using-Stored-Procedure-in-ASPNet.aspx



    https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-in-ASPNet-using-C-and-VBNet.aspx



    https://www.aspsnippets.com/Articles/Insert-Save-Multiple-rows-from-DataGridView-to-Database-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx



    Basically I am trying to import not null table data into table with excel file.










    share|improve this question


























      0












      0








      0








      working with sql and .net, i had a situation to import bulk data with excel file and huge amount of data.



      The table had also 20+ columns some of then are not mandatory.



      the table structure is like below :



      CREATE TABLE [dbo].[Employee] (
      [EmployeeID] INT IDENTITY (1, 1) NOT NULL,
      [EmpID] NVARCHAR (10) NULL,
      [isActive] BIT DEFAULT ((0)) NOT NULL,
      [Fname] NVARCHAR (50) NOT NULL,
      [Mname] NVARCHAR (10) NULL,
      [Lname] NVARCHAR (50) NULL,
      [DOB] DATE NOT NULL,
      [DOJ] DATE NOT NULL,
      [gender] NVARCHAR (10) NOT NULL,
      [M_Status] NVARCHAR (10) NULL,
      [Father_Name] NVARCHAR (50) NULL,
      [Mother_Name] NVARCHAR (50) NULL,
      [Address1] NVARCHAR (20) NULL,
      [Address2] NVARCHAR (20) NULL,
      [Address3] NVARCHAR (20) NULL,
      [Pin_Code] NUMERIC (6) NULL,
      [StateID] INT NULL,
      [DistrictID] INT NULL,
      [Mobile1] NUMERIC (10) NOT NULL,
      [mobile2] NUMERIC (10) NULL,
      [email_address] NVARCHAR (50) NULL,
      [IFSC_Code] NCHAR (11) NULL,
      [bank_account] NCHAR (18) NULL,
      [parmanent_account] NVARCHAR (10) NULL,
      [aadhar_number] NVARCHAR (12) NOT NULL,
      [ESIC] NVARCHAR (10) NULL,
      [UAN] NVARCHAR (10) NULL,
      [SalaryID] INT NOT NULL,
      [DivisionID] INT NOT NULL,
      [BranchID] INT NOT NULL,
      [last_used] INT NOT NULL,
      [access_time] DATETIME NOT NULL,
      PRIMARY KEY CLUSTERED ([EmployeeID] ASC));


      after that i like to import only not null fields and display the Excel data into gridview and then upload the data to table.



      my gridview code is look like :



      <div class="container blue">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="myGridClass" >
      <Columns>
      <asp:BoundField DataField="Emp_ID" HeaderText="Employee ID" />
      <asp:BoundField DataField="First_name" HeaderText="First Name" />
      <asp:BoundField DataField="Last_Name" HeaderText="Last Name" />
      <asp:BoundField DataField="Gender" HeaderText="Male/Female" />
      <asp:BoundField DataField="Date_Birth" HeaderText="Date of Birth" DataFormatString="0:dd/MM/yyyy" />
      <asp:BoundField DataField="Father_Name" HeaderText="Father Name" />
      <asp:BoundField DataField="Mobile" HeaderText="Mobile No." />
      <asp:BoundField DataField="AADHAR_Number" HeaderText="AADHAR No." />
      <asp:BoundField DataField="Date_Joining" HeaderText="Date of Joining" DataFormatString="0:dd/MM/yyyy" />
      <asp:BoundField DataField="UAN" HeaderText="UAN" />
      <asp:BoundField DataField="ESIC" HeaderText="ESIC" />
      <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Salary">
      <EditItemTemplate>
      <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="SalaryID" Mode="Edit" />
      </EditItemTemplate>
      <ItemTemplate>
      <asp:DropDownList ID="Salary_Select" runat="server" CssClass="dropbox" DataSourceID="Salary_List" DataTextField="Post_Name" DataValueField="SalaryID">
      </asp:DropDownList>
      <asp:SqlDataSource ID="Salary_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Post_Name], [SalaryID] FROM [Salary]"></asp:SqlDataSource>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Division">
      <EditItemTemplate>
      <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="DivisionID" Mode="Edit" />
      </EditItemTemplate>
      <ItemTemplate>
      <asp:DropDownList ID="Division_Select" runat="server" CssClass="dropbox" DataSourceID="Division_List" DataTextField="Division_Name" DataValueField="DivisionID" AutoPostBack="True">
      </asp:DropDownList>
      <asp:SqlDataSource ID="Division_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Division_Name], [Company_ID], [DivisionID] FROM [Division]"></asp:SqlDataSource>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Branch">
      <EditItemTemplate>
      <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="BranchID" Mode="Edit" />
      </EditItemTemplate>
      <ItemTemplate>
      <asp:DropDownList ID="Branch_Select" runat="server" CssClass="dropbox" DataSourceID="branch_List" DataTextField="Branch_Name" DataValueField="BranchID">
      </asp:DropDownList>
      <asp:SqlDataSource ID="branch_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [BranchID], [CompanyID], [DivisionID], [Branch_Name] FROM [Branch] WHERE ([DivisionID] = @DivisionID)">
      <SelectParameters>
      <asp:ControlParameter ControlID="Division_Select" Name="DivisionID" PropertyName="SelectedValue" Type="Int32" />
      </SelectParameters>
      </asp:SqlDataSource>
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="salary_source" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [EmployeeID], [Fname], [Mname], [Lname], [DOB], [gender], [Father_Name], [Mobile1], [email_address], [IFSC_Code], [bank_account], [parmanent_account], [aadhar_number], [ESIC], [UAN], [EmpID], [Mother_Name], [DistrictID], [M_Status] FROM [Employee]"></asp:SqlDataSource>
      </div>


      now the import code come into effect.



      Public Sub Import_Employees(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String, gdview As GridView)
      Dim conStr As String = ""
      Select Case Extension
      Case ".xls"
      'Excel 97-03
      conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
      Exit Select
      Case ".xlsx"
      'Excel 07
      conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
      Exit Select
      End Select
      conStr = String.Format(conStr, FilePath, isHDR)
      Dim connExcel As New OleDbConnection(conStr)
      Dim cmdExcel As New OleDbCommand()
      Dim oda As New OleDbDataAdapter()
      Dim dt As New DataTable()
      cmdExcel.Connection = connExcel
      'Get the name of First Sheet
      connExcel.Open()
      Dim dtExcelSchema As DataTable
      dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
      Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
      connExcel.Close()
      'Read Data from First Sheet
      connExcel.Open()
      cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
      oda.SelectCommand = cmdExcel
      oda.Fill(dt)
      connExcel.Close()
      'Bind Data to GridView
      'GridView1.Caption = Path.GetFileName(FilePath)
      gdview.DataSource = dt
      gdview.DataBind()
      End Sub


      function is called on page like below :



      If FileUpload1.HasFile Then
      Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
      Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
      Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
      Dim FilePath As String = Server.MapPath(FolderPath + FileName)
      FileUpload1.SaveAs(FilePath)
      cls.Import_Employees(FilePath, Extension, 0, GridView1)
      Upload.Visible = True
      'Import_To_Grid(FilePath, Extension, 0)
      End If


      I tried some of link below not work for me.



      https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-using-Stored-Procedure-in-ASPNet.aspx



      https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-in-ASPNet-using-C-and-VBNet.aspx



      https://www.aspsnippets.com/Articles/Insert-Save-Multiple-rows-from-DataGridView-to-Database-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx



      Basically I am trying to import not null table data into table with excel file.










      share|improve this question














      working with sql and .net, i had a situation to import bulk data with excel file and huge amount of data.



      The table had also 20+ columns some of then are not mandatory.



      the table structure is like below :



      CREATE TABLE [dbo].[Employee] (
      [EmployeeID] INT IDENTITY (1, 1) NOT NULL,
      [EmpID] NVARCHAR (10) NULL,
      [isActive] BIT DEFAULT ((0)) NOT NULL,
      [Fname] NVARCHAR (50) NOT NULL,
      [Mname] NVARCHAR (10) NULL,
      [Lname] NVARCHAR (50) NULL,
      [DOB] DATE NOT NULL,
      [DOJ] DATE NOT NULL,
      [gender] NVARCHAR (10) NOT NULL,
      [M_Status] NVARCHAR (10) NULL,
      [Father_Name] NVARCHAR (50) NULL,
      [Mother_Name] NVARCHAR (50) NULL,
      [Address1] NVARCHAR (20) NULL,
      [Address2] NVARCHAR (20) NULL,
      [Address3] NVARCHAR (20) NULL,
      [Pin_Code] NUMERIC (6) NULL,
      [StateID] INT NULL,
      [DistrictID] INT NULL,
      [Mobile1] NUMERIC (10) NOT NULL,
      [mobile2] NUMERIC (10) NULL,
      [email_address] NVARCHAR (50) NULL,
      [IFSC_Code] NCHAR (11) NULL,
      [bank_account] NCHAR (18) NULL,
      [parmanent_account] NVARCHAR (10) NULL,
      [aadhar_number] NVARCHAR (12) NOT NULL,
      [ESIC] NVARCHAR (10) NULL,
      [UAN] NVARCHAR (10) NULL,
      [SalaryID] INT NOT NULL,
      [DivisionID] INT NOT NULL,
      [BranchID] INT NOT NULL,
      [last_used] INT NOT NULL,
      [access_time] DATETIME NOT NULL,
      PRIMARY KEY CLUSTERED ([EmployeeID] ASC));


      after that i like to import only not null fields and display the Excel data into gridview and then upload the data to table.



      my gridview code is look like :



      <div class="container blue">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="myGridClass" >
      <Columns>
      <asp:BoundField DataField="Emp_ID" HeaderText="Employee ID" />
      <asp:BoundField DataField="First_name" HeaderText="First Name" />
      <asp:BoundField DataField="Last_Name" HeaderText="Last Name" />
      <asp:BoundField DataField="Gender" HeaderText="Male/Female" />
      <asp:BoundField DataField="Date_Birth" HeaderText="Date of Birth" DataFormatString="0:dd/MM/yyyy" />
      <asp:BoundField DataField="Father_Name" HeaderText="Father Name" />
      <asp:BoundField DataField="Mobile" HeaderText="Mobile No." />
      <asp:BoundField DataField="AADHAR_Number" HeaderText="AADHAR No." />
      <asp:BoundField DataField="Date_Joining" HeaderText="Date of Joining" DataFormatString="0:dd/MM/yyyy" />
      <asp:BoundField DataField="UAN" HeaderText="UAN" />
      <asp:BoundField DataField="ESIC" HeaderText="ESIC" />
      <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Salary">
      <EditItemTemplate>
      <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="SalaryID" Mode="Edit" />
      </EditItemTemplate>
      <ItemTemplate>
      <asp:DropDownList ID="Salary_Select" runat="server" CssClass="dropbox" DataSourceID="Salary_List" DataTextField="Post_Name" DataValueField="SalaryID">
      </asp:DropDownList>
      <asp:SqlDataSource ID="Salary_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Post_Name], [SalaryID] FROM [Salary]"></asp:SqlDataSource>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Division">
      <EditItemTemplate>
      <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="DivisionID" Mode="Edit" />
      </EditItemTemplate>
      <ItemTemplate>
      <asp:DropDownList ID="Division_Select" runat="server" CssClass="dropbox" DataSourceID="Division_List" DataTextField="Division_Name" DataValueField="DivisionID" AutoPostBack="True">
      </asp:DropDownList>
      <asp:SqlDataSource ID="Division_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [Division_Name], [Company_ID], [DivisionID] FROM [Division]"></asp:SqlDataSource>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Branch">
      <EditItemTemplate>
      <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="BranchID" Mode="Edit" />
      </EditItemTemplate>
      <ItemTemplate>
      <asp:DropDownList ID="Branch_Select" runat="server" CssClass="dropbox" DataSourceID="branch_List" DataTextField="Branch_Name" DataValueField="BranchID">
      </asp:DropDownList>
      <asp:SqlDataSource ID="branch_List" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [BranchID], [CompanyID], [DivisionID], [Branch_Name] FROM [Branch] WHERE ([DivisionID] = @DivisionID)">
      <SelectParameters>
      <asp:ControlParameter ControlID="Division_Select" Name="DivisionID" PropertyName="SelectedValue" Type="Int32" />
      </SelectParameters>
      </asp:SqlDataSource>
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="salary_source" runat="server" ConnectionString="<%$ ConnectionStrings:iPayConnectionString %>" SelectCommand="SELECT [EmployeeID], [Fname], [Mname], [Lname], [DOB], [gender], [Father_Name], [Mobile1], [email_address], [IFSC_Code], [bank_account], [parmanent_account], [aadhar_number], [ESIC], [UAN], [EmpID], [Mother_Name], [DistrictID], [M_Status] FROM [Employee]"></asp:SqlDataSource>
      </div>


      now the import code come into effect.



      Public Sub Import_Employees(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String, gdview As GridView)
      Dim conStr As String = ""
      Select Case Extension
      Case ".xls"
      'Excel 97-03
      conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
      Exit Select
      Case ".xlsx"
      'Excel 07
      conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
      Exit Select
      End Select
      conStr = String.Format(conStr, FilePath, isHDR)
      Dim connExcel As New OleDbConnection(conStr)
      Dim cmdExcel As New OleDbCommand()
      Dim oda As New OleDbDataAdapter()
      Dim dt As New DataTable()
      cmdExcel.Connection = connExcel
      'Get the name of First Sheet
      connExcel.Open()
      Dim dtExcelSchema As DataTable
      dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
      Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
      connExcel.Close()
      'Read Data from First Sheet
      connExcel.Open()
      cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
      oda.SelectCommand = cmdExcel
      oda.Fill(dt)
      connExcel.Close()
      'Bind Data to GridView
      'GridView1.Caption = Path.GetFileName(FilePath)
      gdview.DataSource = dt
      gdview.DataBind()
      End Sub


      function is called on page like below :



      If FileUpload1.HasFile Then
      Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
      Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
      Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
      Dim FilePath As String = Server.MapPath(FolderPath + FileName)
      FileUpload1.SaveAs(FilePath)
      cls.Import_Employees(FilePath, Extension, 0, GridView1)
      Upload.Visible = True
      'Import_To_Grid(FilePath, Extension, 0)
      End If


      I tried some of link below not work for me.



      https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-using-Stored-Procedure-in-ASPNet.aspx



      https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-in-ASPNet-using-C-and-VBNet.aspx



      https://www.aspsnippets.com/Articles/Insert-Save-Multiple-rows-from-DataGridView-to-Database-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx



      Basically I am trying to import not null table data into table with excel file.







      .net sql-server excel import






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 16:53









      Atul SharmaAtul Sharma

      238 bronze badges




      238 bronze badges

























          0






          active

          oldest

          votes














          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );














          draft saved

          draft discarded
















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55403033%2fhow-to-import-excel-file-data-in-sql-table-with-not-null-column-only-with-stored%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55403033%2fhow-to-import-excel-file-data-in-sql-table-with-not-null-column-only-with-stored%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현