Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
created table type alredy and how can i insert and display the whole data in grid?

here is my stored procedure-createtype_perdraw is my table type
SQL
CREATE PROCEDURE SP_PERDRAWT
@tblperdraw createtype_perdraw READONLY
 AS
 BEGIN
SET NOCOUNT ON
INSERT INTO mytable(name,address ,age)
SELECT * FROM @tblperdraw
END


here am passing sp and dt1 is my data table

C#
public void bulk_insert()
       {

           using (SqlConnection con = new SqlConnection(@"Dat=D:\MyPractice\browse_fie\browse_fie\browse.mdf;Integrated Security=True"))
           {
               using (SqlCommand cmd = new SqlCommand("SP_PERDRAWT"))
               {

                   cmd.CommandType = CommandType.StoredProcedure;

                   cmd.Connection = con;

                   cmd.Parameters.AddWithValue("@tblperdraw ", dt1);   // dt1 getting all values

                   con.Open();

                   cmd.ExecuteNonQuery();


                   con.Close();

               }

           }

       }



and am getting this error in bulk insert()




An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: The procedure "SP_PERDRAWT" has no parameter named "@tblperdraw ".



pls help me
Posted

It looks like yuo have an extra space in the end of the variable name. Instead of
C#
cmd.Parameters.AddWithValue("@tblperdraw ", dt1);

try
C#
cmd.Parameters.AddWithValue("@tblperdraw", dt1);
 
Share this answer
 
Comments
[no name] 14-Jul-15 7:02am    
wow!!!!!!!!!!!!!!!! its working thanks!! I have another doubt yet. can I able to pass update and delete in this same stored procedure using flag?how
Wendelius 14-Jul-15 7:09am    
You're welcome.

You can always add more parameters to your SP and based on the values of those parameters you can do whatever you like inside the procedure.
[no name] 14-Jul-15 7:20am    
okay am I able to create rest of the things with if(@flag=''delete')?
Wendelius 14-Jul-15 7:35am    
I'm not quite sure what you mean but you can add parameters to the stored procedure or you can create additional columns in your datatable if you want to flag each row separately.

I suggest posting a completely new question and describe the whole situation what you're after.
Remove the extra space after @tblperdraw
cmd.Parameters.AddWithValue("@tblperdraw " <-- here
 
Share this answer
 
the problem is there is no such datatype as createtype_perdraw, you will need to pass the tablename with a valid datatype, prob. varchar is best

SQL
CREATE PROCEDURE SP_PERDRAWT
@tblperdraw varchar(10)


and then use that to build a SQL string that you execute in the stored procedure

SQL
DECLARE @cmd AS NVARCHAR(max)
SET @cmd = N'SELECT (name, address, age)
                 INTO mytable
                 FROM ' + @tblperdraw

EXEC sp_executesql @cmd
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900