Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cannot insert the value NULL into column

Author  Topic 

roshdia
Starting Member

2 Posts

Posted - 2009-04-10 : 12:52:38
I am doing a content management system as final year project. For this, I need to create aspx page dynamically and store the page name into the database. When I try to insert the page name, it gives the error even though the value is not null.

Server Error in '/Main Edit' Application.
Cannot insert the value NULL into column 'Name', table 'C:\DOCUMENTS AND SETTINGS\ROSHNI\MY DOCUMENTS\VISUAL STUDIO 2008\WEBSITES\MAIN EDIT\APP_DATA\PAGEINFO.MDF.dbo.Page'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Name', table 'C:\DOCUMENTS AND SETTINGS\ROSHNI\MY DOCUMENTS\VISUAL STUDIO 2008\WEBSITES\MAIN EDIT\APP_DATA\PAGEINFO.MDF.dbo.Page'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Source Error:

Line 60: }
Line 61: PageDataSource.Insert();
Line 62: Response.Redirect("pages/" + Title + ".aspx", true);


Source File: c:\Documents and Settings\Roshni\My Documents\Visual Studio 2008\WebSites\Main Edit\CreatePage.aspx.cs Line: 62

Stack Trace:

[SqlException (0x80131904): Cannot insert the value NULL into column 'Name', table 'C:\DOCUMENTS AND SETTINGS\ROSHNI\MY DOCUMENTS\VISUAL STUDIO 2008\WEBSITES\MAIN EDIT\APP_DATA\PAGEINFO.MDF.dbo.Page'; column does not allow nulls. INSERT fails.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1948826
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4844747
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +204
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +175
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +386
System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values) +227
System.Web.UI.WebControls.SqlDataSource.Insert() +16
CreatePage.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\Roshni\My Documents\Visual Studio 2008\WebSites\Main Edit\CreatePage.aspx.cs:62
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053


The code used to do this is as follows:
CreatePage.aspx file contains this:

<asp:SqlDataSource ID="PageDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:PageInfoConnectionString %>"
InsertCommand="INSERT INTO Page(Name) VALUES (@Text)"
SelectCommand="SELECT Page.* FROM Page">
<InsertParameters>
<asp:FormParameter Name="Text" FormField="txtPage" />
</InsertParameters>
</asp:SqlDataSource>

CreatePage.aspx.cs contains this:

protected void Button1_Click(object sender, EventArgs e)
{
string root = Server.MapPath("~");
string Template = root + "\\Template.temp";
StringBuilder line = new StringBuilder();
using (StreamReader rwOpenTemplate = new StreamReader(Template))
{
while (!rwOpenTemplate.EndOfStream)
{
line.Append(rwOpenTemplate.ReadToEnd());
}
}
//int ID = 0;
string SaveFilePath = "";
string SaveFileName = "";
//Random ran = new Random();
//ID = ran.Next();
//Page Name Creator with only URL allowed character
string Title = StripURLNotAllowedChars(txtPage.Text);
SaveFileName = "\\" + Title + ".aspx";
SaveFilePath = root + "\\Pages\\" + SaveFileName;
FileStream fsSave = File.Create(SaveFilePath);
if (line != null)
{ //Replace the page content
line.Replace("[Title]", txtPage.Text.Replace("<", "<").Replace(">", ">").Replace('"', ' ').Replace('"', ' '));
//line.Replace("[ID]", ID.ToString());
StreamWriter sw = null;
try
{
sw = new StreamWriter(fsSave);
sw.Write(line);
}
catch (Exception ex)
{
Label2.Text = ex.Message;
}
finally
{
sw.Close();
}
PageDataSource.Insert();
Response.Redirect("pages/" + Title + ".aspx", true);
}
}

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-04-10 : 14:14:50
Try doing a trace, and get the value for @Text. It would seem to me, the value is NULL, since SQL Server is throwing that error.

Perhaps run your page in debug mode and step through the code?

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

roshdia
Starting Member

2 Posts

Posted - 2009-04-10 : 14:22:59
The text was fine. The problem was with FormParameters. I had to use ControlParameters and it worked!!
Go to Top of Page
   

- Advertisement -