Reputation: 9927
I have a SQL table like this:
ID
is Identity column.
And I have a class for this table in ASP.Net project like this:
public class Topic
{
public int ModuleID { get; set; }
public string Name { get; set; }
public string BodyHTML { get; set; }
}
Also I have a stored procedure:
ALTER PROCEDURE [dbo].[addTopic]
@Topic dbo.Topic READONLY
AS
BEGIN
insert into OnlineHelp_Topic
select * from @Topic
END
I want to pass a Topic
object from project and execute this stored procedure by SqlHelper.
I read many post like this and create a User-Defind Table
in SQL Server and I test this code:
Topic Topic = new Topic()
{
BodyHTML = "test body",
ModuleID = 1000,
Name = "test"
};
string constr = ConfigurationManager.ConnectionStrings["testConnection"].ConnectionString;
int rowsAffected = SqlHelper.ExecuteNonQuery(constr, "addTopic", new SqlParameter("@Topic", Topic));
But this code has error in SqlHelper.ExecuteNonQuery(...)
like this;
It would be very helpful if someone could explain solution for this problem.
Upvotes: 2
Views: 5730
Reputation: 1
You need to pass a DataTable as parameter example
SqlHelper.ExecuteNonQuery(constr, "addTopic", new SqlParameter("@Topic", TopicDatatable));
The DataTable must have the same columns: names and datatypes of the User Defined Table Type.
Upvotes: 0
Reputation: 3349
Try to pass the topic parameters like follows:
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@ModuleID ", topic.ModuleID ));
parameters.Add(new SqlParameter("@Name", topic.Name));
parameters.Add(new SqlParameter("@BodyHTML", topic.BodyHTML));
int rowsAffected = SqlHelper.ExecuteNonQuery(constr, "addTopic", parameters.ToArray());
You will also need to modify your stored procedure.
Upvotes: 1