I
Imran Aziz
Hello All,
I have a table with few fields that allow null, I use the following code
to add entries
strQuery = "insert into tblChannelItems
(nChannelID,sItemTitle,sItemLink,sItemDesc,sItemAuthor,sItemCategory,sItemComments,sItemEnclosure,sItemPubDate)
values
(@nChannelID,@sItemTitle,@sItemLink,@sItemDesc,@sItemAuthor,@sItemCategory,@sItemComments,@sItemEnclosure,@sItemPubDate);";
strQuery = strQuery + " SELECT nItemID from tblChannelItems where
nItemID=SCOPE_IDENTITY();";
SqlCommand objCmd = new SqlCommand(strQuery, this.conn);
SqlParameter nChannelID = objCmd.Parameters.Add("@nChannelID",
SqlDbType.Int);
SqlParameter sItemTitle = objCmd.Parameters.Add("@sItemTitle",
SqlDbType.NVarChar);
SqlParameter sItemLink = objCmd.Parameters.Add("@sItemLink",
SqlDbType.NVarChar);
SqlParameter sItemDesc = objCmd.Parameters.Add("@sItemDesc",
SqlDbType.Text);
SqlParameter sItemAuthor = objCmd.Parameters.Add("@sItemAuthor",
SqlDbType.NVarChar);
SqlParameter sItemCategory = objCmd.Parameters.Add("@sItemCategory",
SqlDbType.NVarChar);
SqlParameter sItemComments = objCmd.Parameters.Add("@sItemComments",
SqlDbType.NVarChar);
SqlParameter sItemEnclosure = objCmd.Parameters.Add("@sItemEnclosure",
SqlDbType.NVarChar);
SqlParameter sItemPubDate = objCmd.Parameters.Add("@sItemPubDate",
SqlDbType.DateTime);
nChannelID.Value = objStItem.nChannelID;
sItemTitle.Value = objStItem.sItemTitle;
sItemLink.Value = objStItem.sItemLink;
sItemDesc.Value = objStItem.sItemDesc;
sItemAuthor.Value = objStItem.sItemAuthor;
sItemCategory.Value = objStItem.sItemCategory;
sItemComments.Value = objStItem.sItemComments;
sItemEnclosure.Value = objStItem.sItemEnclosure;
sItemPubDate.Value = objStItem.sItemPubDate;
nItemID = (int)objCmd.ExecuteScalar();
but I get the error of
{"Prepared statement '(@nChannelID int,@sItemTitle nvarchar(33),@sItemLink
nvarchar(50' expects parameter @sItemAuthor, which was not supplied."}
Seems although the author column is optional but still somehow since I dont
specify the parameters as optional in my prepared statement so I get this
error, can anyone please guide me how I can set these parameters as optional
, so I dont get this error?
Imran.
I have a table with few fields that allow null, I use the following code
to add entries
strQuery = "insert into tblChannelItems
(nChannelID,sItemTitle,sItemLink,sItemDesc,sItemAuthor,sItemCategory,sItemComments,sItemEnclosure,sItemPubDate)
values
(@nChannelID,@sItemTitle,@sItemLink,@sItemDesc,@sItemAuthor,@sItemCategory,@sItemComments,@sItemEnclosure,@sItemPubDate);";
strQuery = strQuery + " SELECT nItemID from tblChannelItems where
nItemID=SCOPE_IDENTITY();";
SqlCommand objCmd = new SqlCommand(strQuery, this.conn);
SqlParameter nChannelID = objCmd.Parameters.Add("@nChannelID",
SqlDbType.Int);
SqlParameter sItemTitle = objCmd.Parameters.Add("@sItemTitle",
SqlDbType.NVarChar);
SqlParameter sItemLink = objCmd.Parameters.Add("@sItemLink",
SqlDbType.NVarChar);
SqlParameter sItemDesc = objCmd.Parameters.Add("@sItemDesc",
SqlDbType.Text);
SqlParameter sItemAuthor = objCmd.Parameters.Add("@sItemAuthor",
SqlDbType.NVarChar);
SqlParameter sItemCategory = objCmd.Parameters.Add("@sItemCategory",
SqlDbType.NVarChar);
SqlParameter sItemComments = objCmd.Parameters.Add("@sItemComments",
SqlDbType.NVarChar);
SqlParameter sItemEnclosure = objCmd.Parameters.Add("@sItemEnclosure",
SqlDbType.NVarChar);
SqlParameter sItemPubDate = objCmd.Parameters.Add("@sItemPubDate",
SqlDbType.DateTime);
nChannelID.Value = objStItem.nChannelID;
sItemTitle.Value = objStItem.sItemTitle;
sItemLink.Value = objStItem.sItemLink;
sItemDesc.Value = objStItem.sItemDesc;
sItemAuthor.Value = objStItem.sItemAuthor;
sItemCategory.Value = objStItem.sItemCategory;
sItemComments.Value = objStItem.sItemComments;
sItemEnclosure.Value = objStItem.sItemEnclosure;
sItemPubDate.Value = objStItem.sItemPubDate;
nItemID = (int)objCmd.ExecuteScalar();
but I get the error of
{"Prepared statement '(@nChannelID int,@sItemTitle nvarchar(33),@sItemLink
nvarchar(50' expects parameter @sItemAuthor, which was not supplied."}
Seems although the author column is optional but still somehow since I dont
specify the parameters as optional in my prepared statement so I get this
error, can anyone please guide me how I can set these parameters as optional
, so I dont get this error?
Imran.