Tuesday, March 13, 2012

Simple guestbook: problem inserting new data.

Hello everyone,

I wanted to write a very simple guestbook but I am having some trouble adding the data to the database.

 <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:dbsysConnectionString%>" InsertCommand ="INSERT INTO guestbook(Email, Message, Name) VALUES (@dotnet.itags.org.Email, @dotnet.itags.org.Boodschap, @dotnet.itags.org.Naam)"> <insertparameters> <asp:FormParameter Name="Email" FormField="TxtEmail"/> <asp:FormParameter Name="Boodschap" FormField="TxtBoodschap" /> <asp:FormParameter Name="Naam" FormField="TxtNaam" /> </insertparameters> </asp:SqlDataSource>

I have three textboxes on my page (TxtEmail, TxtBoodschap & TxtNaam) and a submit button with the following event handler :

 Protected Sub BtnVersturen(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSent.Click SqlDataSource1.Insert() Response.Redirect("registraties.aspx") End Sub

So whenever I click this button, the data from these fields should be entered in a SQL Server 2005 database.When I try this, I get no error but although a new row is created in the table in my database, the data itself is not added (so only NULL values are added to the database).

Is there something wrong with this configuration of my database or is the problem situated somewhere else?

Thanks in advance,
Maxime

your SqlDataSource ID isSqlDataSource

try this

Protected Sub BtnVersturen(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles BtnSent.Click SqlDataSource.Insert() Response.Redirect("registraties.aspx")End Sub

Please remember to Mark As Answer if this post answered your question!

Try this

Protected Sub BtnVersturen(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSent.Click

SqlDataSource.InsertParameters("Email").DefaultValue = TxtEmail.Text
SqlDataSource.InsertParameters("Boodschap").DefaultValue =TxtBoodschap.Text
SqlDataSource.InsertParameters("Naam").DefaultValue = TxtNaam.Text
SqlDataSource.Insert()

Response.Redirect("registraties.aspx")
End Sub

Thanks


The SqlDataSource versus SqlDataSource1 was not the issue, I made a mistake while copying to this thread.

E_screw, thanks for your reply, but I am getting the following error while trying to submit the data :

System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object." Source="App_Web_kobz2cuu" StackTrace: at gastenboek_default.BtnVersturen(Object sender, EventArgs e) in C:\Documents and Settings\Maxime Muylle\Mijn documenten\Websites\definitief DBsys\gastenboek\default.aspx.vb:line 5 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Does anybody know why this is not working?

Thanks in advance,
Maxime


Could you keep a break point and check which is throwing the null reference exception? Also, your BtnVersturen is handling BtnSent.Click. Check if its the correct event handler or not.

Thanks


I would suggest having a breakpoint so that we can narrow down at the issue with more granular view.

Maxime Muylle:

The System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Web_kobz2cuu"
StackTrace:
at gastenboek_default.BtnVersturen(Object sender, EventArgs e) in C:\Documents and Settings\Maxime Muylle\Mijn documenten\Websites\definitief DBsys\gastenboek\default.aspx.vb:line 5
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Hi Maxime,

Based on my experience, you got the above error MOSTLY because you did not create the instance of your button successfully. Could you please check if you have constructed you button class and add it into your page? Hope my suggestion helps :)

0 comments:

Post a Comment