Question

Unable to import XLS into SQL database through DataSet and TableAdapter

Asked by: bobbellows

I am adapting a tutorial to import an excel spreadsheet into an SQL database. I am new to the whole procedure but have made some great progress. I am using a DataSet and TableAdapter to accomplish the task. The reader is working as I can use a counter routine to stop it at a certain point, or if I don't it will display the last record. First a check is made to see if a record already exists with a student_id. If not, the record is inserted into the database. Reccords aren't being inserted. I started to troubleshoot the problem and came across this error in the code behind. I figure it's a good place to start but I don't know enough about the syntax to correct it. This is the line:  
                StudentARecordNumber = Convert.ToInt32(FinaoAdapter.InsertStudentInfoAQuery(student_id, last_name, first_name, middle_name, Gender, birthdate, Grade, ethnicity, lep, SPED, Sheltered, AIP, AVID, ELL, notes, He_She, His_Hers, Him_Her))
This is the error VisualStudio gives me:
Public Overridable Function InsertStudentInfoAQuery() As Interger' has no parameters and its return type cannot be indexed. Any suggestions? below is some of the code behind.

Dim StudentARecordNumber As Integer = 0
        Try
            Dim FinaoAdapter As New StudentInfoADataSetTableAdapters.StudentInfoA_localTableAdapter
            Dim FinaoDataTable As StudentInfoADataSet.StudentInfoA_localDataTable = Nothing
            FinaoDataTable = FinaoAdapter.GetStudentInfoADataByRecordNumber(StudentARecordNumber)
 
            ' see if the student already exists in the table, if not insert it
            If Not FinaoDataTable Is Nothing Then
                If FinaoDataTable.Rows.Count > 0 Then
                    If Not FinaoDataTable(0).StudentARecordNumber = Nothing Then
                        StudentARecordNumber = FinaoDataTable(0).StudentARecordNumber
                        LabelImport.Text = LabelImport.Text & "<fontcolor=lime>Member Found, Not Imported: " & "ID: " & student_id & " " & last_name & ", " & first_name & ".</Font><br />"
 
                    End If
                End If
            End If
            If StudentARecordNumber = 0 Then
                ' retrieve the identity key StudentARecordNumber from the insert
                StudentARecordNumber = Convert.ToInt32(FinaoAdapter.InsertStudentInfoAQuery(student_id, last_name, first_name, middle_name, Gender, birthdate, Grade, ethnicity, lep, SPED, Sheltered, AIP, AVID, ELL, notes, He_She, His_Hers, Him_Her))

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-20 at 20:10:04ID24829268
Tags

SQL

,

EXCEL

,

XLS

,

DataSet

,

TableAdapter

,

VisualStudio 2008

,

VB

Topics

MS SQL Server

,

Visual Basic Programming

,

SQL Query Syntax

,

Microsoft Excel Spreadsheet Software

,

Visual Studio

Participating Experts
2
Points
500
Comments
24

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Dataset tableadapter question
    I have a form that upon opening a combobox populates from a dataset with a tableadapter linked to it. Then i have another dataadapter linked to the dataset (same table) with a different query textbox that is binding to the same table in the dataset. I am using this command ...
  2. tableadapter strongly typed dataset
    I have a strongly typed dataset. I create tableadapters in the designer. My db structure has changed and I need the table adapter and table to reflect these changes. 1. How do I do this? 2. If my query has some select statement like select col1,col2,col3 where id=@id can I...
  3. TableAdapters and custom DataSets
    Hi, I am making a 3 tier application with strongly typed tableAdapters to connect to the database. In my DataLayer I have made custom methods to Create,Read,Update and Delete rows in the tables (e.g. getAllTests). Normally they return a strongly typed DataTable (like testsDat...
  4. DataSet Designer & own TableAdapter
    Is it possible to create your own TableAdapter that you can just drag and drop onto the DataSet Designer like the toolbox TableAdapter? And if so, could someone please show me a sample implementation of this or give some clear instructions on how to go about it? So far I ha...
  5. Using Reflection to list typed datasets, tableadapters, tab…
    I have an assembly (dll) that contains a bunch of typed datasets. I want to list out (on the console is fine) each dataset name, its associated tableadapters and the commandtext of each command in the tableadapter's command collection. I'm having some luck listing the types c...
  6. Adding a 6th tableadapter to a dataset
    When I add a 6th tableadapter to a dataset I get the following error: Reference to a non-shared member requires an object reference. Any ideas?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: northfieldsPosted on 2009-10-20 at 22:07:28ID: 25620928

Hi

Can you post  details about InsertStudentInfoAQuery(), how was it defined, it looks like it is throwing the error because InsertStudentInfoAQuery() doesnt take any arguments, even though you specify them in the snippet.

 

by: bobbellowsPosted on 2009-10-21 at 04:27:37ID: 25622782

northfields,
Thank you for your reply. I'm somewhat new to SQL and this is my first attempt at DataSets and TableAdapters. The Query is a TableAdapter query and I've attached its code. I think this is what you mean - if not let me know.
Thanks

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="StudentInfoADataSet" targetNamespace="http://tempuri.org/StudentInfoADataSet.xsd" xmlns:mstns="http://tempuri.org/StudentInfoADataSet.xsd" xmlns="http://tempuri.org/StudentInfoADataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:annotation>
    <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
      <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
        <Connections>
          <Connection AppSettingsObjectName="Web.config" AppSettingsPropertyName="FINAOsmsSQL1mdf_92291StudentInfoDataSetConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="FINAOsmsSQL1mdf_92291StudentInfoDataSetConnectionString (Web.config)" ParameterPrefix="@" PropertyReference="AppConfig.System.Configuration.ConfigurationManager.0.ConnectionStrings.FINAOsmsSQL1mdf_92291StudentInfoDataSetConnectionString.ConnectionString" Provider="System.Data.SqlClient" />
        </Connections>
        <Tables>
          <TableAdapter BaseClass="System.ComponentModel.Component" DataAccessorModifier="AutoLayout, AnsiClass, Class, Public" DataAccessorName="StudentInfoA_localTableAdapter" GeneratorDataComponentClassName="StudentInfoA_localTableAdapter" Name="StudentInfoA_local" UserDataComponentName="StudentInfoA_localTableAdapter">
            <MainSource>
              <DbSource ConnectionRef="FINAOsmsSQL1mdf_92291StudentInfoDataSetConnectionString (Web.config)" DbObjectName="[FINAOsmsSQL1.mdf_92291].dbo.StudentInfoA_local" DbObjectType="Table" GenerateMethods="Get" GenerateShortCommands="false" GeneratorGetMethodName="GetStudentInfoAData" GetMethodModifier="Public" GetMethodName="GetStudentInfoAData" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="false" UserGetMethodName="GetStudentInfoAData" UserSourceName="GetStudentInfoAData">
                <SelectCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="true">
                    <CommandText>SELECT        StudentInfoA_local.*
FROM            StudentInfoA_local</CommandText>
                    <Parameters />
                  </DbCommand>
                </SelectCommand>
              </DbSource>
            </MainSource>
            <Mappings>
              <Mapping SourceColumn="student_id" DataSetColumn="student_id" />
              <Mapping SourceColumn="last_name" DataSetColumn="last_name" />
              <Mapping SourceColumn="first_name" DataSetColumn="first_name" />
              <Mapping SourceColumn="middle_name" DataSetColumn="middle_name" />
              <Mapping SourceColumn="Gender" DataSetColumn="Gender" />
              <Mapping SourceColumn="birthdate" DataSetColumn="birthdate" />
              <Mapping SourceColumn="Grade" DataSetColumn="Grade" />
              <Mapping SourceColumn="ethnicity" DataSetColumn="ethnicity" />
              <Mapping SourceColumn="lep" DataSetColumn="lep" />
              <Mapping SourceColumn="SPED" DataSetColumn="SPED" />
              <Mapping SourceColumn="Sheltered" DataSetColumn="Sheltered" />
              <Mapping SourceColumn="AIP" DataSetColumn="AIP" />
              <Mapping SourceColumn="AVID" DataSetColumn="AVID" />
              <Mapping SourceColumn="ELL" DataSetColumn="ELL" />
              <Mapping SourceColumn="notes" DataSetColumn="notes" />
              <Mapping SourceColumn="StudentARecordNumber" DataSetColumn="StudentARecordNumber" />
              <Mapping SourceColumn="He_She" DataSetColumn="He_She" />
              <Mapping SourceColumn="His_Hers" DataSetColumn="His_Hers" />
              <Mapping SourceColumn="Him_Her" DataSetColumn="Him_Her" />
            </Mappings>
            <Sources>
              <DbSource ConnectionRef="FINAOsmsSQL1mdf_92291StudentInfoDataSetConnectionString (Web.config)" DbObjectName="[FINAOsmsSQL1.mdf_92291].dbo.StudentInfoA_local" DbObjectType="Table" GenerateMethods="Get" GenerateShortCommands="true" GeneratorGetMethodName="GetStudentInfoADataByRecordNumber" GeneratorSourceName="FillBy" GetMethodModifier="Public" GetMethodName="GetStudentInfoADataByRecordNumber" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="true" UserGetMethodName="GetStudentInfoADataByRecordNumber" UserSourceName="GetStudentInfoADataByRecordNumber">
                <SelectCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="true">
                    <CommandText>SELECT        student_id, StudentARecordNumber
FROM            StudentInfoA_local
WHERE        (student_id = @student_id)</CommandText>
                    <Parameters>
                      <Parameter AllowDbNull="false" AutogeneratedName="student_id" ColumnName="student_id" DataSourceName="[FINAOsmsSQL1.mdf_92291].dbo.StudentInfoA_local" DataTypeServer="int" DbType="Int32" Direction="Input" ParameterName="@student_id" Precision="0" ProviderType="Int" Scale="0" Size="4" SourceColumn="student_id" SourceColumnNullMapping="false" SourceVersion="Current" />
                    </Parameters>
                  </DbCommand>
                </SelectCommand>
              </DbSource>
              <DbSource ConnectionRef="FINAOsmsSQL1mdf_92291StudentInfoDataSetConnectionString (Web.config)" DbObjectName="" DbObjectType="Unknown" GenerateShortCommands="true" GeneratorGetMethodName="GetDataBy" GeneratorSourceName="InsertStudentInfoAQuery" Modifier="Public" Name="InsertStudentInfoAQuery" QueryType="NoData" ScalarCallRetval="System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="true" UserGetMethodName="GetDataBy" UserSourceName="InsertStudentInfoAQuery">
                <InsertCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="true">
                    <CommandText>INSERT into [StudentInfoA_local] (student_id, last_name, first_name, middle_name, Gender, birthdate, Grade, ethnicity, lep, SPED, Sheltered, AIP, AVID, ELL, notes, He_She, His_Hers, Him_Her)
VALUES (@student_id, @ last_name, @ first_name, @ middle_name, @ Gender, @ birthdate, @ Grade, @ ethnicity, @ lep, @ SPED, @ Sheltered, @ AIP, @ AVID, @ ELL, @ notes, @ He_She, @ His_Hers, @ Him_Her)
 
-- return the StudentARecordNumber
SELECT SCOPE_IDENTITY()</CommandText>
                    <Parameters />
                  </DbCommand>
                </InsertCommand>
              </DbSource>
            </Sources>
          </TableAdapter>
        </Tables>
        <Sources />
      </DataSource>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="StudentInfoADataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_UserDSName="StudentInfoADataSet" msprop:Generator_DataSetName="StudentInfoADataSet">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="StudentInfoA_local" msprop:Generator_UserTableName="StudentInfoA_local" msprop:Generator_RowDeletedName="StudentInfoA_localRowDeleted" msprop:Generator_RowChangedName="StudentInfoA_localRowChanged" msprop:Generator_RowClassName="StudentInfoA_localRow" msprop:Generator_RowChangingName="StudentInfoA_localRowChanging" msprop:Generator_RowEvArgName="StudentInfoA_localRowChangeEvent" msprop:Generator_RowEvHandlerName="StudentInfoA_localRowChangeEventHandler" msprop:Generator_TableClassName="StudentInfoA_localDataTable" msprop:Generator_TableVarName="tableStudentInfoA_local" msprop:Generator_RowDeletingName="StudentInfoA_localRowDeleting" msprop:Generator_TablePropName="StudentInfoA_local">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="student_id" msprop:Generator_UserColumnName="student_id" msprop:Generator_ColumnPropNameInRow="student_id" msprop:Generator_ColumnVarNameInTable="columnstudent_id" msprop:Generator_ColumnPropNameInTable="student_idColumn" type="xs:int" />
              <xs:element name="last_name" msprop:Generator_UserColumnName="last_name" msprop:Generator_ColumnPropNameInRow="last_name" msprop:Generator_ColumnVarNameInTable="columnlast_name" msprop:Generator_ColumnPropNameInTable="last_nameColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="255" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="first_name" msprop:Generator_UserColumnName="first_name" msprop:Generator_ColumnPropNameInRow="first_name" msprop:Generator_ColumnVarNameInTable="columnfirst_name" msprop:Generator_ColumnPropNameInTable="first_nameColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="255" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="middle_name" msprop:Generator_UserColumnName="middle_name" msprop:Generator_ColumnPropNameInRow="middle_name" msprop:Generator_ColumnVarNameInTable="columnmiddle_name" msprop:Generator_ColumnPropNameInTable="middle_nameColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="255" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Gender" msprop:Generator_UserColumnName="Gender" msprop:Generator_ColumnPropNameInRow="Gender" msprop:Generator_ColumnVarNameInTable="columnGender" msprop:Generator_ColumnPropNameInTable="GenderColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="255" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="birthdate" msprop:Generator_UserColumnName="birthdate" msprop:Generator_ColumnPropNameInRow="birthdate" msprop:Generator_ColumnVarNameInTable="columnbirthdate" msprop:Generator_ColumnPropNameInTable="birthdateColumn" type="xs:dateTime" minOccurs="0" />
              <xs:element name="Grade" msprop:Generator_UserColumnName="Grade" msprop:Generator_ColumnPropNameInRow="Grade" msprop:Generator_ColumnVarNameInTable="columnGrade" msprop:Generator_ColumnPropNameInTable="GradeColumn" type="xs:double" minOccurs="0" />
              <xs:element name="ethnicity" msprop:Generator_UserColumnName="ethnicity" msprop:Generator_ColumnPropNameInRow="ethnicity" msprop:Generator_ColumnVarNameInTable="columnethnicity" msprop:Generator_ColumnPropNameInTable="ethnicityColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="10" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="lep" msprop:Generator_UserColumnName="lep" msprop:Generator_ColumnPropNameInRow="lep" msprop:Generator_ColumnVarNameInTable="columnlep" msprop:Generator_ColumnPropNameInTable="lepColumn" type="xs:boolean" minOccurs="0" />
              <xs:element name="SPED" msprop:Generator_UserColumnName="SPED" msprop:Generator_ColumnPropNameInRow="SPED" msprop:Generator_ColumnVarNameInTable="columnSPED" msprop:Generator_ColumnPropNameInTable="SPEDColumn" type="xs:boolean" minOccurs="0" />
              <xs:element name="Sheltered" msprop:Generator_UserColumnName="Sheltered" msprop:Generator_ColumnPropNameInRow="Sheltered" msprop:Generator_ColumnVarNameInTable="columnSheltered" msprop:Generator_ColumnPropNameInTable="ShelteredColumn" type="xs:boolean" minOccurs="0" />
              <xs:element name="AIP" msprop:Generator_UserColumnName="AIP" msprop:Generator_ColumnPropNameInRow="AIP" msprop:Generator_ColumnVarNameInTable="columnAIP" msprop:Generator_ColumnPropNameInTable="AIPColumn" type="xs:boolean" minOccurs="0" />
              <xs:element name="AVID" msprop:Generator_UserColumnName="AVID" msprop:Generator_ColumnPropNameInRow="AVID" msprop:Generator_ColumnVarNameInTable="columnAVID" msprop:Generator_ColumnPropNameInTable="AVIDColumn" type="xs:boolean" minOccurs="0" />
              <xs:element name="ELL" msprop:Generator_UserColumnName="ELL" msprop:Generator_ColumnPropNameInRow="ELL" msprop:Generator_ColumnVarNameInTable="columnELL" msprop:Generator_ColumnPropNameInTable="ELLColumn" type="xs:boolean" minOccurs="0" />
              <xs:element name="notes" msprop:Generator_UserColumnName="notes" msprop:Generator_ColumnPropNameInRow="notes" msprop:Generator_ColumnVarNameInTable="columnnotes" msprop:Generator_ColumnPropNameInTable="notesColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="2147483647" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="StudentARecordNumber" msdata:ReadOnly="true" msdata:AutoIncrement="true" msdata:AutoIncrementSeed="-1" msdata:AutoIncrementStep="-1" msprop:Generator_UserColumnName="StudentARecordNumber" msprop:Generator_ColumnPropNameInRow="StudentARecordNumber" msprop:Generator_ColumnVarNameInTable="columnStudentARecordNumber" msprop:Generator_ColumnPropNameInTable="StudentARecordNumberColumn" type="xs:int" />
              <xs:element name="He_She" msprop:Generator_UserColumnName="He_She" msprop:Generator_ColumnPropNameInRow="He_She" msprop:Generator_ColumnVarNameInTable="columnHe_She" msprop:Generator_ColumnPropNameInTable="He_SheColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="10" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="His_Hers" msprop:Generator_UserColumnName="His_Hers" msprop:Generator_ColumnPropNameInRow="His_Hers" msprop:Generator_ColumnVarNameInTable="columnHis_Hers" msprop:Generator_ColumnPropNameInTable="His_HersColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="10" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Him_Her" msprop:Generator_UserColumnName="Him_Her" msprop:Generator_ColumnPropNameInRow="Him_Her" msprop:Generator_ColumnVarNameInTable="columnHim_Her" msprop:Generator_ColumnPropNameInTable="Him_HerColumn" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="10" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:StudentInfoA_local" />
      <xs:field xpath="mstns:student_id" />
    </xs:unique>
  </xs:element>
</xs:schema>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:

Select allOpen in new window

 

by: mdouganPosted on 2009-10-25 at 09:43:03ID: 25657188

There shouldn't be spaces after the @ signs in this statement

                    <CommandText>INSERT into [StudentInfoA_local] (student_id, last_name, first_name, middle_name, Gender, birthdate, Grade, ethnicity, lep, SPED, Sheltered, AIP, AVID, ELL, notes, He_She, His_Hers, Him_Her)
VALUES (@student_id, @ last_name, @ first_name, @ middle_name, @ Gender, @ birthdate, @ Grade, @ ethnicity, @ lep, @ SPED, @ Sheltered, @ AIP, @ AVID, @ ELL, @ notes, @ He_She, @ His_Hers, @ Him_Her)

It should look like

                    <CommandText>INSERT into [StudentInfoA_local] (student_id, last_name, first_name, middle_name, Gender, birthdate, Grade, ethnicity, lep, SPED, Sheltered, AIP, AVID, ELL, notes, He_She, His_Hers, Him_Her)
VALUES (@student_id, @last_name, @first_name, @middle_name, @Gender, @birthdate, @Grade, @ethnicity, @lep, @SPED, @Sheltered, @AIP, @AVID, @ELL, @notes, @He_She, @His_Hers, @Him_Her)

 

by: bobbellowsPosted on 2009-10-25 at 20:54:49ID: 25659585

mdougan,
Thank you for your reply. I made the change you suggested - I can't believe I missed the spaces. However, that didn't help. The error code is in my code behind for the page. I attached a snippit in my original. I've attached the entire code to this comment. I've attached a txt file with the complete compilation. Again, thanks.

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Partial Class SiteAdministrator_ImportExcel
    Inherits System.Web.UI.Page
    Protected Sub ButtonUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonUpload.Click
        PanelUpload.Visible = True
        PanelView.Visible = False
        PanelImport.Visible = False
    End Sub
    Protected Function ExcelConnection() As OleDbCommand
 
        ' Connect to the Excel Spreadsheet
        Dim xConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & (Server.MapPath("~/ExcelImport.xls")) & ";" & "Extended Properties=Excel 8.0;"
 
        ' create your excel connection object using the connection string
        Dim objXConn As New OleDbConnection(xConnStr)
        objXConn.Open()
 
        ' use a SQL Select command to retrieve the data from the Excel Spreadsheet
        ' the "table name" is the name of the worksheet within the spreadsheet
        ' in this case, the worksheet name is "Bellows3" and is coded as: [Bellows3$]
        Dim objCommand As New OleDbCommand("SELECT * FROM [Bellows3$]", objXConn)
        Return objCommand
 
    End Function
 
    Protected Sub ButtonUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ButtonUploadFile.Click
        If FileUploadExcel.HasFile Then
            Try
                ' alter path for your project
                FileUploadExcel.SaveAs(Server.MapPath("~/ExcelImport.xls"))
                LabelUpload.Text = "Upload File Name: " & _
                FileUploadExcel.PostedFile.FileName & "<br>" & _
                "Type: " & _
                FileUploadExcel.PostedFile.ContentType & _
                " File Size: " & _
                FileUploadExcel.PostedFile.ContentLength & " kb<br>"
            Catch ex As Exception
                LabelUpload.Text = "Error: " & ex.Message.ToString
            End Try
        Else
            LabelUpload.Text = "Please select a file to upload."
        End If
    End Sub
 
    Protected Sub ButtonView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonView.Click
        PanelUpload.Visible = False
        Panelview.Visible = True
        Panelimport.Visible = False
 
        ' Create a new Adapter
        Dim objDataAdapter As New OleDbDataAdapter()
 
        ' retrieve the Select command for the Spreadsheet
        objDataAdapter.SelectCommand = ExcelConnection()
 
        ' Create a DataSet
        Dim objDataSet As New DataSet()
 
        ' Populate the DataSet with the spreadsheet worksheet data
        objDataAdapter.Fill(objDataSet)
 
        ' Bind the data to the GridView
        GridViewExcel.DataSource = objDataSet.Tables(0).DefaultView
        GridViewExcel.DataBind()
 
 
 
    End Sub
    Protected Sub ButtonImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles ButtonImport.Click
        PanelUpload.Visible = False
        PanelView.Visible = False
        PanelImport.Visible = True
        LabelImport.Text = "" ' reset to blank
 
        ' retrieve the Select Command for the worksheet data
        Dim objCommand As New OleDbCommand()
        objCommand = ExcelConnection()
 
        '  create a DataReader
        Dim reader As OleDbDataReader
        reader = objCommand.ExecuteReader()
 
 
        Dim counter As Integer = 0 ' used for testing your import in smaller increments
 
        While reader.Read()
 
            counter = counter + 1 ' counter to exit early for testing...
 
            Dim student_id As String = Convert.ToString(reader("SID"))
            Dim last_name As String = Convert.ToString(reader("LN"))
            Dim first_name As String = Convert.ToString(reader("FN"))
            Dim middle_name As String = Convert.ToString(reader("MN"))
            Dim Gender As String = Convert.ToString(reader("GENDER"))
            Dim birthdate As Date = Convert.ToString(reader("DOB"))
            Dim Grade As String = Convert.ToString(reader("GRADE"))
            Dim ethnicity As String = Convert.ToString(reader("ETHNICITY"))
            Dim lep As String = Convert.ToString(reader("LEP"))
            Dim SPED As String = Convert.ToString(reader("SPED"))
            Dim Sheltered As String = Convert.ToString(reader("SHELTERED"))
            Dim AIP As String = Convert.ToString(reader("AIP"))
            Dim AVID As String = Convert.ToString(reader("AVID"))
            Dim ELL As String = Convert.ToString(reader("ELL"))
            Dim notes As String = Convert.ToString(reader("NOTES"))
            Dim StudentARecordNumber As Integer = 0
            Dim He_She As String = Convert.ToString(reader("HE"))
            Dim His_Hers As String = Convert.ToString(reader("HIS"))
            Dim Him_Her As String = Convert.ToString(reader("HIM"))
 
            'TODO: Insert any required validtions here...
 
            'TODO: Insert a record into StudentInfoA table (if it's not a duplicate)
 
            StudentARecordNumber = ImportIntoStudentInfoA(student_id, last_name, first_name, middle_name, Gender, birthdate, Grade, ethnicity, lep, SPED, Sheltered, AIP, AVID, ELL, notes, He_She, His_Hers, Him_Her)
 
            'Display information in LabelImport
            LabelImport.Text = student_id & " " & last_name & ", " & first_name & " " & middle_name & " " & Gender & " " & birthdate & " " & Grade & " " & ethnicity & " " & lep & " " & SPED & " " & Sheltered & " " & AIP & " " & AVID & " " & ELL & " " & notes & " " & StudentARecordNumber & " " & He_She & " " & His_Hers & " " & Him_Her & " " & "<br>"
 
            'If counter > 2 Then ' exit early for testing, comment later...
            'Exit While
            ' End If
 
        End While
        reader.Close()
    End Sub
 
    Protected Function ImportIntoStudentInfoA(ByVal student_id As String, ByVal last_name As String, ByVal first_name As String, ByVal middle_name As String, ByVal Gender As String, ByVal birthdate As String, ByVal Grade As String, ByVal ethnicity As String, ByVal lep As String, ByVal SPED As String, ByVal Sheltered As String, ByVal AIP As String, ByVal AVID As String, ByVal ELL As String, ByVal notes As String, ByVal He_She As String, ByVal His_Hers As String, ByVal Him_Her As String) As Integer
 
 
        ' make sure values don't exceed column limits
        student_id = Left(student_id, 10)
        last_name = Left(last_name, 50)
        first_name = Left(first_name, 50)
        middle_name = Left(middle_name, 50)
        Gender = Left(Gender, 10)
        birthdate = Left(birthdate, 50)
        Grade = Left(Grade, 10)
        ethnicity = Left(ethnicity, 10)
        lep = Left(lep, 5)
        SPED = Left(SPED, 5)
        Sheltered = Left(Sheltered, 5)
        AIP = Left(AIP, 5)
        AVID = Left(AVID, 5)
        ELL = Left(ELL, 5)
        notes = Left(notes, 5)
        He_She = Left(He_She, 10)
        His_Hers = Left(His_Hers, 10)
        Him_Her = Left(Him_Her, 10)
 
        Dim StudentARecordNumber As Integer = 0
        Try
            Dim FinaoAdapter As New StudentInfoADataSetTableAdapters.StudentInfoA_localTableAdapter
            Dim FinaoDataTable As StudentInfoADataSet.StudentInfoA_localDataTable = Nothing
            FinaoDataTable = FinaoAdapter.GetStudentInfoADataByRecordNumber(StudentARecordNumber)
 
            ' see if the student already exists in the table, if not insert it
            If Not FinaoDataTable Is Nothing Then
                If FinaoDataTable.Rows.Count > 0 Then
                    If Not FinaoDataTable(0).StudentARecordNumber = Nothing Then
                        StudentARecordNumber = FinaoDataTable(0).StudentARecordNumber
                        LabelImport.Text = LabelImport.Text & "<fontcolor=lime>Member Found, Not Imported: " & "ID: " & student_id & " " & last_name & ", " & first_name & ".</Font><br />"
 
                    End If
                End If
            End If
            If StudentARecordNumber = 0 Then
                ' retrieve the identity key StudentARecordNumber from the insert
                StudentARecordNumber = Convert.ToInt32(FinaoAdapter.InsertStudentInfoAQuery(student_id, last_name, first_name, middle_name, Gender, birthdate, Grade, ethnicity, lep, SPED, Sheltered, AIP, AVID, ELL, notes, He_She, His_Hers, Him_Her))
 
                LabelImport.Text = LabelImport.Text & "<fontcolor=lime>Member Imported: " & "ID: " & student_id & " " & last_name & ", " & first_name & " Grade: " & Grade & " Gender: " & Gender & ".</font><br />"
 
            End If
 
            Return StudentARecordNumber
        Catch ex As Exception
            LabelImport.Text &= "<fontcolor=red>" & ex.ToString & "</font><br />"
            Return 0
        End Try
 
 
 
    End Function
End Class

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:

Select allOpen in new window

 

by: mdouganPosted on 2009-10-26 at 06:29:42ID: 25662005

OK, but we were in the right ballpark.  If you go back to that same block of code, where you fixed the spaces, then look at the section above, where the SELECT statement is defined, you have the CommandText section where the select SQL is listed with one parameter, the student_id.  Directly below that, there is this parameters section:

                    <Parameters>
                      <Parameter AllowDbNull="false" AutogeneratedName="student_id" ColumnName="student_id" DataSourceName="[FINAOsmsSQL1.mdf_92291].dbo.StudentInfoA_local" DataTypeServer="int" DbType="Int32" Direction="Input" ParameterName="@student_id" Precision="0" ProviderType="Int" Scale="0" Size="4" SourceColumn="student_id" SourceColumnNullMapping="false" SourceVersion="Current" />
                    </Parameters>

You need a similar section for your INSERT statement, but right now, your parameters section is empty.  It just looks like   <Parameters />

You're going to need a <Parameter block for each of the variables that you listed with an @ sign.

 

by: bobbellowsPosted on 2009-10-27 at 05:12:50ID: 25671459

mdougan,
Thanks for your reply. I'll try and get to that tonight. My "day job" has me working a lot of overtime this week so it's hard to squeeze time on this project. Until this project starts generating income of course I'll have to stay with my real job. I'll let you know how it went after I work on it some tonight. Again, thanks for your patience.

 

by: mdouganPosted on 2009-10-27 at 11:17:20ID: 25675641

No worries.  

By the way, there are more efficient ways to do this.  One way is to have all the code in a Stored Procedure in SQL Server.  You would call the stored proc from your VB program, and the proc would do a bulk load of the XLS spreadsheet (actually, it would be better to transform it into a Tab-delimited text file), into a temporary table, then you could insert into your permanent table where the student id NOT Exists.

 

by: bobbellowsPosted on 2009-10-27 at 16:51:57ID: 25678891

This sounds more simple than the way I'm doing it. I am very used to working with databases and converting information to tab delimited. I can imagine it would be faster as well. If this is really more efficient, I am willing to learn how this works. If there is a place you can point me to for a tutorial on how to do this, I'll start studying and working on it. In the mean time I'll try and fix this one I've got going. Thanks.

 

by: mdouganPosted on 2009-10-28 at 05:27:15ID: 25682427

Here is a mocked up example of doing the bulk insert from inside of a stored procedure.  Just a few caveats, the tab delimited text file needs to be stored in a folder that is publicly shared so that Everyone can read it.  That's because your SQL Server database is going to need to read it to import it, and it won't be logged in as you.  I use the UNC naming convention, but if your SQL Server is running on the same computer, you could use your regular drive mappings... C:\myfolder\myfile.txt

You need to insure that your spreadsheet has the exact number of columns you define in the #loaded_data table.  If you have columns with a null value in them, then ensure that the cell in the spreadsheet has the word  NULL in it.  When you convert the spreadsheet to tab delimited text, do not have it wrap text in double or single quotes.

After the BULK INSERT statement, I just gave simplified examples of what you might want to do, logic-wize, you should revise as necessary.

CREATE PROC dbo.load_students
AS BEGIN
 
-- define a temp table with proper data types for each column
-- in the spreadsheet
CREATE TABLE [#loaded_data] (
             [student_id]    int,
             [last_name]     varchar(200),
             [first_name]    varchar(200),
             [date_of_birth] smalldatetime
)
-- Make sure that you delete the header row in the spreadsheet if any
-- Save the spreadsheet as a Tab delimited text file in the shared folder below
 
-- This command reads the spreadsheet and inserts into a temp table
    EXECUTE( 'BULK INSERT #loaded_data ' +
             'FROM ''\\mycomputer\myfolder\myfile.txt''' )
 
-- Now, delete from the temp table, records that already exist
-- in the student table
    DELETE FROM #loaded_data 
          WHERE EXISTS (SELECT students.student_id 
                          FROM students, #loaded_data
                         WHERE #loaded_data.student_id =  
                               students.student_id)
 
-- Now, go ahead and insert the new student records
    INSERT INTO students
    SELECT student_id, last_name, first_name, date_of_birth
      FROM #loaded_data
 
-- Now, return a recordset of all the identity columns to the program
-- Or, don't do this, and simply refresh your original dataset in the 
-- program
    SELECT S.student_id, S.student_record_number
      FROM students S
      JOIN #loaded_data 
        ON #loaded_data.student_id = S.student_id
 
-- This should happen automatically, but I do it just to be safe
    DROP TABLE [#loaded_data]
 
END
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:

Select allOpen in new window

 

by: bobbellowsPosted on 2009-10-28 at 21:47:28ID: 25690733

This is so awesome!
I created the stored procedure. I called the procedure from GridView in VisualStudio 2008. When I went to test the procedure I received the error that I didn't have permission to use the bulk load statement. Do I need to set the permissions in Web.config or does this have to be set it with my web host? I can't believe how simple this was compared to the tutorial I was using.

 

by: mdouganPosted on 2009-10-29 at 04:28:35ID: 25692439

That's a database user permission. In your connection string, do you login as a particular user? If so you need to grant that permission to that user. That's probably something the host provider will have to do for you. Just tell them you want that user to be able to run a bulk insert, and for them to grant the permission.

 

by: bobbellowsPosted on 2009-10-29 at 05:17:08ID: 25692749

mdougan,
I contacted my web hosting company this morning. For secuity reasons they don't allow permissions above dbo. Any suggestions?

 

by: mdouganPosted on 2009-10-29 at 07:15:56ID: 25693903

It shouldn't have to be above DBO, in fact, I think you (the user you are logging in as) just have to be a member of the DBO group.  But it has been a couple of years since I used this, and I'm fuzzy on the details.  I seem to remember that our DBA only had to grant us those permissions on the temp database, where we are creating the temp table, and they didn't think that was too much of a security issue.

If they know exactly what is required, and it's not allowed by their security practices, then you can't use this approach.

This bulk load is the fastest, easiest and most efficient way to do what you are trying to do.  But the second best approach would be to send your entire dataset up to the stored procedure, then do all of the lookups up in the Proc, rather than down in the client code, as you are now.  I think that a SQLDataset has a property called XML that you can use to dump the contents, in XML format, into a string, which you can then pass to the stored proc.  There are SQL commands that allow you to create a temp table through reading an XML string (but I'd have to give that to you on another day... I'm busy moving today!).

 

by: bobbellowsPosted on 2009-11-01 at 05:24:56ID: 25713627

mdougan,
I hope your move-in goes smoothly. We are so close to having this work I can taste it. I upgraded to a Virtual Server which allowed me the bulk insert and bulk load commands. This is how I am testing my insert.
1. Created a new table in the database titled ExcelImportSmall.
2. Created columns for student_id, last_name, first-name, date_of-birth.
3. Saved the tab delimited file as ExcelImportSmall2.txt
4. Saved the txt file again with only the first ten rows as ExcelImportSmall1.txt
5. Uploaded the txt files to the server - per instructions from my web host.
6. Made the necessary directory changes.
7. Created a web page that called the stored procedure.
8. Changed the name of ExcelImportSmall1.txt to just ExcelImportSmall.txt.
9. Ran the page (procedure). IT WORKED PERFECTLY! I opened the table in SQL Server 2005 and the 10 rows were there.
10 Deleted the 10 rows.
11. Renamed ExcelImportSmall.txt back to ExcelImportSmall1.txt
12 Renamed ExcelImportSmall2.txt to ExcelImportSmall.txt
13. Ran the page (procedure). IT WORKED PERFECTLY! I opened the table in SQL Server 2005 and the 183 rows were there.
14. Deleted the 183 rows.
15. Renamed ExcelImportSmall.txt back to ExcelImportSmall2.txt.
16. Did steps 8 and 9 again. The 10 rows were there again.
17 Did steps 11 and 12 again LEAVING THE 10 ROWS IN THE TABLE.
18. Ran the page (procedure).
It didn't work. The ten rows were there but the table wasn't appended with the other 173 rows. Ran the test several times. I even closed the SQL Server and reopened it so I would have a fresh view of the table but only the original 10 rows were there. I've attached the code of the stored procedure below. I hope you can find what I did wrong. Thanks.

USE [FINAOsmsSQL1_94132]
GO
/****** Object:  StoredProcedure [dbo].[load_students]    Script Date: 11/01/2009 06:21:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROC [dbo].[load_students]
	-- Add the parameters for the stored procedure here
	--<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
	--<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	--SET NOCOUNT ON;
 
    -- Insert statements for procedure here
	--SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
 
-- define a temp table with proper data types for each column
-- in the spreadsheet
CREATE TABLE [#loaded_data] (
             [student_id]    int,
             [last_name]     varchar(200),
             [first_name]    varchar(200),
             [date_of_birth] smalldatetime
)
-- Make sure that you delete the header row in the spreadsheet if any
-- Save the spreadsheet as a Tab delimited text file in the shared folder below
 
-- This command reads the spreadsheet and inserts into a temp table
    EXECUTE( 'BULK INSERT #loaded_data ' +
             'FROM ''c:\db\rbellows\ExcelImportSmall.txt''' )
 
-- Now, delete from the temp table, records that already exist
-- in the student table
    DELETE FROM #loaded_data 
          WHERE EXISTS (SELECT ExcelImportSmall.student_id 
                          FROM ExcelImportSmall, #loaded_data
                         WHERE #loaded_data.student_id =  
                               ExcelImportSmall.student_id)
 
-- Now, go ahead and insert the new student records
    INSERT INTO ExcelImportSmall
    SELECT student_id, last_name, first_name, date_of_birth
      FROM #loaded_data
 
-- Now, return a recordset of all the identity columns to the program
-- Or, don't do this, and simply refresh your original dataset in the 
-- program
    --SELECT S.student_id, S.student_record_number
      --FROM students S
      --JOIN #loaded_data 
        --ON #loaded_data.student_id = S.student_id
 
-- This should happen automatically, but I do it just to be safe
    DROP TABLE [#loaded_data]
 
END
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:

Select allOpen in new window

 

by: mdouganPosted on 2009-11-01 at 08:19:42ID: 25714192

Your proc and methodology look good. Are you sure the student I'd is unique in the spreadsheet? It's possible that the proc is encountering an error and you are not catching it. After each code block such as the bulk insert, delete, insert and select you should reall check the system variable @@error to see if it is > 0, and if so do a raiseerror statement. I can give you an example later (or you can look it up).

The way I debug is to comment everything out except the first code block, then write my test script to display the results of that step. If that works, then I uncomment the second block and test the first and second together and so on until you don't get the results you expected. Then it is easier to pinpoint the solution.

Move went well, but don't have consistent Internet yet.

 

by: mdouganPosted on 2009-11-01 at 12:33:38ID: 25715316

Here is an example of checking the @@error system variable and raising an error if one occurred.

    DECLARE @sql_error      int

   -- this block is whatever you are doing... insert, update, bulk insert
    INSERT INTO collections (
         part_name_no,
         coll_name_no,
         orig_name_no)
    VALUES (
      @part_name_no,
      @coll_name_no,
      @orig_name_no)

    -- you must do this immediately after executing the statement you are interested in, otherwise the value of @@error will change
    SET @sql_error = @@error

    -- this causes a level 16 error which generates a runtime error in the program calling the proc
    IF  @sql_error <> 0 BEGIN
        RAISERROR( 'Error inserting Collection Part: %d', 16, 1, @sql_error )
        RETURN @sql_error
    END                      

    RETURN 0

One other thought that I had, was that it is possible that SQL Server is caching your text file, and re-using the cached version the second time you upload.  You can test this by doing your bulk insert into the #loaded_data table, then selecting all rows from that table and displaying them on your test webpage.  If you know that you renamed the files correctly, but are still getting the same rows, then SQL Server is using the cached version.

There is a system stored procedure which forces SQL Server to Flush the Cache, which you could execute at the top of your proc, but I can't remember the name of the proc, nor have an example, but you might be able to google that.

 

by: bobbellowsPosted on 2009-11-13 at 04:23:33ID: 25813301

mdougan,

I finally finished a project at work that took away some of my overtime and allowed me time to get back to this issue. Thank you for your patience. I ran the error check as you suggested. It returned no errors.

To make testing easier I uploaded 3 text files to import. The first one is ExcelImportSmall1.txt contains only the first 10 records of the 183 total. ExcelImportSmall2 contains the remaining 173 records of the total 183. The third one contains all 183 records.

I imported the first 10. It worked perfectly. I imported the remaining 173 and it appended the table so it had all 183 records. I deleted all 183 records. and imported the first ten again -- it worked. Went to import the ExcelImportSmall3 to see if it would delete the first ten from the temp table before inserting into the db table. Nothing was appended to the db table.

Below is the message thread of the sp execution. It appears it created and loaded all 183 records into the table. Then it deleted the 183 records so there were 0 records to insert.  

(183 row(s) affected)

(183 row(s) affected)

(0 row(s) affected)

(1 row(s) affected)

 

by: mdouganPosted on 2009-11-13 at 05:08:59ID: 25813562

Well, two possibilities, that what is getting populated in the student_id column isn't what you expect.  If you load, say, the 10 records, then actually go look in the database table, is the student_id column populated?  Or is it blank?  If it is blank, ,then there might be an extra blank column at the head of your text file... could there be an extra comma in the front of each row?

If not, then we might need to alter the delete statement slightly, try this:

    DELETE FROM #loaded_data
          WHERE EXISTS (SELECT ExcelImportSmall.student_id
                          FROM ExcelImportSmall
                         WHERE #loaded_data.student_id =  
                               ExcelImportSmall.student_id)

I just removed #loaded_data from the SELECT statement, so that the JOIN is actually happening to the #loaded_data referenced in the DELETE statement.  That's really the way it should be done anyway.
 

 

by: bobbellowsPosted on 2009-11-14 at 09:58:20ID: 25821725

mdougan,
Your modified delete statement worked. I modified the working proc we were using to include all the columns of info I had to import. They imported perfectly. Since they were the first students and I know there were no duplicate ID's I can't attest to that part of the routine working. I won't be able to test that until I import other student rosters that might have some duplicates to those loaded. I have 2 final questions as we close out this issue.

1.I now see how much simpler stored procedures are to write and manage. Is there a book or website that has collections of stored procedures so as I continue to learn their power I won't have to start from scratch?  If so, could you give me some names? Thanks.

2. In case this proc runs into a bump in the future, when I go to load other rosters, is there someway I can contact you so I won't have to start all over again with another expert? If so, how? Thanks again.

 

by: bobbellowsPosted on 2009-11-14 at 10:01:47ID: 31643740

mdougan is probably the most patient expert I have encountered in the exchange. I had work issues that didn't let me get back right away -- he was moving -- I was learning about stored procedures -- mdougan was phenominal to hang in with me till we had a solution.

 

by: mdouganPosted on 2009-11-14 at 10:38:52ID: 25821849

Cool!

1. You are correct that it is often much simpler, more efficient and easier to manage code placed in a stored proc, rather than down in a client application, so, I always rely heavily on them.  I save a lot of the procs I've worked on in the past, and I go back to them frequently for the proper syntax.  I haven't really looked for any books on stored procedures.  Generally, if I'm trying to figure out how to do something, then I'll just google it.  There are many forums where you can find sample code.  And, also, Microsoft has some pretty good examples too.

2. Probably the easiest way for you to get my attention is to just add another comment to this question.  I'll get notified by e-mail.  Even if you need help on another question, you can comment here with a link to the new question.  Or, if it is related to this question, just keep the dialog going here, it doesn't matter if the question is officially closed or not.  Or, it looks like there is a link above called 'ask a related question' which might notify me too... I've never tried that one.

Cheers!
Mike

 

by: bobbellowsPosted on 2010-02-25 at 05:02:14ID: 26849532

mdougan,

Mike, I'm working on creating my first report and there was a suggestion about writing a stored procedure. However, I don't know how to do the parsing the expert suggested. If you want to look at it the ID is 25199588

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...