Solved

SQLDataAdapter throwing exception

Posted on 2009-04-01
2
256 Views
Last Modified: 2012-05-06
Hello Experts,

I have the following stored procedure to insert a record in the table

       @EmpId            varchar(20),
      @Department varchar(50),
      @Title            varchar(50),
      @LastName      varchar(50),
      @FirstName      varchar(50),
      @Location      varchar(50),
      @Office            varchar(50),
      @Phone            varchar(25),
      @Fax            varchar(25),
      @Email            varchar(50)l
AS
BEGIN
      SET NOCOUNT ON;

      Insert Into tblEmployees
            (EmpId, Department,Title, EmpLastName, EmpFirstName,Location, Office, Phone, Fax, Email)
      Values
            (@EmpId, @Department, @Title, @LastName, @FirstName, @Location, @Office, @Phone, @Fax, @Email)

I am using this sp as InsertCommand of the SQLDataAdapter. Here is the C# code

con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select EmpId,Department,EmpLastNam as LastName, EmpFirstName as FirstName, Location, Office, Email, Phone, Fax from tblEmployees",con);
                da.InsertCommand = new SqlCommand("InsertEmployeeRecord", con);
                da.InsertCommand.CommandType = CommandType.StoredProcedure;
                da.InsertCommand.Parameters.Add("@EmpId",SqlDbType.VarChar,20);
                da.InsertCommand.Parameters.Add("@Department",SqlDbType.VarChar,50);
                da.InsertCommand.Parameters.Add("@LastName",SqlDbType.VarChar,50);
                da.InsertCommand.Parameters.Add("@FirstName",SqlDbType.VarChar,50);
                da.InsertCommand.Parameters.Add("@Location",SqlDbType.VarChar,50);
                da.InsertCommand.Parameters.Add("@Office",SqlDbType.VarChar,50);
                da.InsertCommand.Parameters.Add("@Email",SqlDbType.VarChar,50);
                da.InsertCommand.Parameters.Add("@Phone",SqlDbType.VarChar,25);
                da.InsertCommand.Parameters.Add("@Fax",SqlDbType.VarChar,25);
                da.Update(table);

The last line throws an exception saying, "Procedure or function 'InsertEmployeeRecord' expects parameter '@EmpId', which was not supplied."

Whereas in the command window, I can see that EMPId is in the table.

>? table.Rows[0]["EmpId"]
"102275"

What am I doing wrong?

Thanks,

JS
0
Comment
Question by:jimmysaunders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24040578
@title is missing
0
 

Accepted Solution

by:
jimmysaunders earned 0 total points
ID: 24040895
Thanks, but that wasn't the reason since it was bombing on EmpID which wasn't missing. I figured out the problem. I had to provide the SourceColumn property while adding the parameter.

da.InsertCommand.Parameters.Add("@EmpId",SqlDbType.VarChar,20,"EmpId")
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 60
Import text from rows into columns? 12 78
Problem to copy file 14 50
Nested forach loop to linq 3 27
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

756 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