Friday, March 30, 2012

Problem with SqlDataSource.Insert() using MasterPage

Hi,

because I have had problems with transporting of data from TextBoxes to SQL database in my application using SqlDataSource.Insert(), I tried to analyze it using Microsoft's sample code located here:

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

Everything worked well untill I have used masterpage. Since this moment my database table started receive only NULL values - the same problem, which I have in my application. Despite all data values were correct before calling SqlDataSource.Insert() method. Is it some bug or something else (my bugSmile) and how can I resolve it? I'd like to use all advantages of masterpages and datasources together.

Here are non-working codes of spoken sample:

shelter3.aspx

<%@.PageLanguage="VB"MasterPageFile="~/MasterPage3.master"AutoEventWireup="false"CodeFile="shelter3.aspx.vb"Inherits="shelter3"Title="Untitled Page" %>

<asp:ContentID="Content1"ContentPlaceHolderID="Obsah"runat="Server">

<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"

DataTextField="nazev"DataValueField="code"/>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:intranet %>"

SelectCommand="SELECT nazev,code FROM smenky"InsertCommand="INSERT INTO smenky (nazev,code) VALUES (@.nazev,@.code)">

<InsertParameters>

<asp:FormParameterName="nazev"FormField="NazevBox"/>

<asp:FormParameterName="code"FormField="CodeBox"/>

</InsertParameters>

</asp:SqlDataSource>

<p>

<asp:TextBoxID="NazevBox"runat="server"/>

<asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ControlToValidate="NazevBox"

Display="Static"ErrorMessage="Please enter a company name."/>

<p>

<asp:TextBoxID="CodeBox"runat="server"/>

<asp:RequiredFieldValidatorID="RequiredFieldValidator2"runat="server"ControlToValidate="CodeBox"

Display="Static"ErrorMessage="Please enter a phone number."/>

<p>

<asp:ButtonID="Button1"runat="server"Text="Insert New Shipper"/>

</asp:Content>

shelter3.aspx.vb

PartialClass shelter3

Inherits System.Web.UI.Page

ProtectedSub InsertShipper(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.Click

SqlDataSource1.Insert()

EndSub

EndClass

masterpage3.master

<%@.MasterLanguage="VB"CodeFile="MasterPage3.master.vb"Inherits="MasterPage3" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:contentplaceholderid="Obsah"runat="server">

</asp:contentplaceholder>

</div>

</form>

</body>

</html>

masterpage3.master.vb

PartialClass MasterPage3

Inherits System.Web.UI.MasterPage

EndClass

Thanks for any idea

I reproduced your issue, not sure what's causing the problem. However we can use asp:ControlParameter in this case:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LocalSqlServer %>"

SelectCommand="SELECT id,name FROM test_MasterSqlDataSource" InsertCommand="insert into test_MasterSqlDataSource select @.id,@.name">

<InsertParameters>
<asp:ControlParameter ControlID="NazevBox" PropertyName="Text" Name="id" />

<asp:ControlParameter Name="name" PropertyName="Text" ControlID="CodeBox" />

</InsertParameters>

</asp:SqlDataSource>

I've tested and found the values can be inserted into database properly.

No comments:

Post a Comment