Solved

reading excel file in asp.net and inserting record in sql server

Posted on 2013-06-15
4
940 Views
Last Modified: 2013-06-17
I have written one application in ASP.Net (C#).

I want to read a excel file and insert all record from excel file to sql server table.
Say for example i have two column in excel file (student_id and student_name). I have 100 rows in this file. Now i want to read all record from this file and insert it into a table in sql server.

For inserting a record i want to write a stored procedure. Currently i am inserting record for executing for loop. so in this case, i am first reading file and populating data to data table variable. then for iterating through each record in table and calling stored procedure to insert that row.

is there any way where by i can send data table to sp as parameter and insert all record from datatable to database.

thanks
0
Comment
Question by:ronipats
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
rpkhare earned 500 total points
ID: 39250159
(1) You first read the Excel file and convert it to XML. To do this refer this link:

How to Convert Excel to XML in C#.NET

(2) Now take an input parameter in your stored procedure of type XML.

(3) Pass XML to stored procedure.

(4) In stored procedure, read this XML file as mentioned in the solution below:

Convert Xml to Table SQL Server
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39250701
If you are using SQL server 2008, you can send the datatable as parameter as shown in the link below: (You have to create a table type  in SQL Server)
http://msdn.microsoft.com/en-us/library/bb675163.aspx
More examples are shown here:
http://stackoverflow.com/questions/2784851/passing-a-datatable-into-a-stored-procedure-is-there-a-better-way
http://www.codeproject.com/Articles/412802/Sending-a-DataTable-to-a-Stored-Procedure

Note: There is no need to use XML conversion.
0
 

Author Comment

by:ronipats
ID: 39252766
i have created one user defined tabletype in sql server 2008.

Then after i have created on stored procedure in which i have declared one parameter of new created type as

ex.
this is how i created new table type

USE examnewdb
GO

-- Create the data type
CREATE TYPE demotbl AS TABLE
(
      course_name varchar(100),
      course_code varchar(50)
)
GO

this is how i created stored procedure

CREATE PROCEDURE demoProc
      -- Add the parameters for the stored procedure here
      @tbl_demotbl demotbl readonly
AS
BEGIN

It is showing me error as "the parameter @tbl_demotbl has an invalid type. It cannot be declared READONLY since it is not a table valued parameter."

what should i do to resolve this error
0
 
LVL 8

Expert Comment

by:rpkhare
ID: 39253906
Please try this and let me know:
CREATE PROCEDURE demoProc 
(
      -- Add the parameters for the stored procedure here
      @tbl_demotbl demotbl readonly
)
AS
BEGIN

Open in new window


Seems to be a case of missing ")".
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now