Friday, February 24, 2012

Inserting TextBox values to database using SqlDataSource.Insert method

Hi, it is few days I posted here my question, but received no answer. Maybe the problem is just my problem, maybe I put my question some strange way. OK, I try to put it again, more simply.

I have few textboxes, their values I need to transport to database. I set SqlDataSource, parameters... and used SqlDataSource.Insert() method. I got NULL values in the database's record. So I tried find problem by using Microsoft's sample code from addresshttp://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insert.aspx. After some changes I tried that code and everything went well, data were put into database. Next step was to separate code beside and structure of page to two separate files followed by new test. Good again, data were delivered to database. Next step: to use MasterPage, very simple, just with one ContentPlaceHolder. After this step the program stoped to deliver data to database and delivers only NULLs to new record. It is exactly the same problem which I have found in my application. The functionless code is here:

http://forums.asp.net/thread/1437716.aspx

I cannot find any answer this problem on forums worldwide. I cannot believe it is only my problem. I compared html code of two generated pages - with maserPage and without. There are differentions in code in ids' of input fields generated by NET.Framework:

Without masterpage:

<input name="NazevBox" type="text" id="NazevBox" />
<span id="RequiredFieldValidator1" style='color:Red;visibility:hidden;'>Please enter a company name.</span>
<p>
<input name="CodeBox" type="text" id="CodeBox" />
<span id="RequiredFieldValidator2" style='color:Red;visibility:hidden;'>Please enter a phone number.</span>
<p>
<input type="submit" name="Button1" value="Insert New Shipper" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />

With masterpage:

<input name="ctl00$Obsah$NazevBox" type="text" id="ctl00_Obsah_NazevBox" />
<span id="ctl00_Obsah_RequiredFieldValidator1" style='color:Red;visibility:hidden;'>Please enter a company name.</span>
<p>
<input name="ctl00$Obsah$CodeBox" type="text" id="ctl00_Obsah_CodeBox" />
<span id="ctl00_Obsah_RequiredFieldValidator2" style='color:Red;visibility:hidden;'>Please enter a phone number.</span>
<p>
<input type="submit" name="ctl00$Obsah$Button1" value="Insert New Shipper" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$Obsah$Button1", "", true, "", "", false, false))" id="ctl00_Obsah_Button1" />

In second case ids' of input fields have different names, but I hope it is inner business of NET.Framework.

There must be something I haven't noticed, maybe NET's bug, maybe my own. Thanks for any suggestion.

Had a look at both of your posts, mostly the first.

By using the Inserting event for the SqlDataSource control, you can programmatically supply the parameters and their values.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.inserting.aspx

|||Thank you for your reply. Your solution works fine (and resolves this bug of NET.Framework 2.0 - I think it is little bug.)

No comments:

Post a Comment