SQL Server Insert Problem

G

Guest

I have created a class and stored procedure to insert records into a table.
The insert works but only the first character of the "Title" and "Body"
fields are added. I have traced up to the point of executing the sproc and
the string values are intact up to this point. Below is my code for the
SPROC and the insert method of the class:

.........................................................................................
Sproc
.........................................................................................
CREATE PROCEDURE spAddBlog
@pTitle varchar,
@pBody varchar,
@pStartdate datetime,
@pEnddate datetime,
@pActive bit
AS
INSERT INTO blog
Values (@pTitle,@pBody,@pStartdate,@pEnddate,@pActive)
GO


.........................................................................................
Class
.........................................................................................
mStartdate = DateTime.Now;
mEnddate = DateTime.Now;
mActive = 1;

/* Connection Object */
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSetting["connString"]);
conn.Open();

/* Command Object */
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "spAddBlog";
comm.CommandType = CommandType.StoredProcedure;

SqlParameter objParam;

objParam = comm.Parameters.Add("@pTitle",SqlDbType.VarChar);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mTitle;

objParam = comm.Parameters.Add("@pBody",SqlDbType.VarChar);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mBody;

objParam = comm.Parameters.Add("@pStartdate",SqlDbType.DateTime);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mStartdate;

objParam = comm.Parameters.Add("@pEnddate",SqlDbType.DateTime);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mEnddate;

objParam = comm.Parameters.Add("@pActive",SqlDbType.Bit);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mActive;

try
{
comm.ExecuteNonQuery();
comm.Dispose();
return true;
}
catch
{
return false;
}
 
H

Hermit Dave

you have to specify the size of varchar... if you dont specify its taken as
just a char
CREATE PROCEDURE spAddBlog
@pTitle varchar(100),
@pBody varchar(3000),
.......

also

objParam = comm.Parameters.Add("@pTitle",SqlDbType.VarChar, 100);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mTitle;

objParam = comm.Parameters.Add("@pBody",SqlDbType.VarChar,3000);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mBody;


--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
NathanV said:
I have created a class and stored procedure to insert records into a table.
The insert works but only the first character of the "Title" and "Body"
fields are added. I have traced up to the point of executing the sproc and
the string values are intact up to this point. Below is my code for the
SPROC and the insert method of the class:

.............................................................................
.............
.............................................................................
.............
CREATE PROCEDURE spAddBlog
@pTitle varchar,
@pBody varchar,
@pStartdate datetime,
@pEnddate datetime,
@pActive bit
AS
INSERT INTO blog
Values (@pTitle,@pBody,@pStartdate,@pEnddate,@pActive)
GO


.............................................................................
.............
.............................................................................
.............
mStartdate = DateTime.Now;
mEnddate = DateTime.Now;
mActive = 1;

/* Connection Object */
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSetting["connString"]);
conn.Open();

/* Command Object */
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "spAddBlog";
comm.CommandType = CommandType.StoredProcedure;

SqlParameter objParam;

objParam = comm.Parameters.Add("@pTitle",SqlDbType.VarChar);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mTitle;

objParam = comm.Parameters.Add("@pBody",SqlDbType.VarChar);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mBody;

objParam = comm.Parameters.Add("@pStartdate",SqlDbType.DateTime);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mStartdate;

objParam = comm.Parameters.Add("@pEnddate",SqlDbType.DateTime);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mEnddate;

objParam = comm.Parameters.Add("@pActive",SqlDbType.Bit);
objParam.Direction = ParameterDirection.Input;
objParam.Value = mActive;

try
{
comm.ExecuteNonQuery();
comm.Dispose();
return true;
}
catch
{
return false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top