Wednesday, March 21, 2012
Problems with ORDER BY clause
If anyone is interrested in answer, there is article on SQLServer Magazine treating this problem:
http://www.winnetmag.com/Article/ArticleID/16495/16495.html
Monday, March 12, 2012
Problems with INSERT parameter
how do I tell a DropDownList, that its value is supposed to be aparameter for a SQLDataSource's InsertParameters? Following is part ofmy aspx code:
<asp:SqlDataSource
ID="srcPerson"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="System.Data.SqlClient"
InsertCommand="InsertPerson"
InsertCommandType="StoredProcedure"
OldValuesParameterFormatString="{0}">
<InsertParameters>
<asp:Parameter Name="PersonLastName" Type="String" />
<asp:Parameter Name="PersonFirstName" Type="String" />
<asp:Parameter Name="RoomID" Type="String" />
<asp:Parameter Name="PersonPhone" Type="String" />
<asp:Parameter Name="PersonPhonePrivate" Type="String" />
<asp:Parameter Name="PersonPhoneMobile" Type="String" />
<asp:Parameter Name="PersonEmail" Type="String" />
</InsertParameters>
</asp:SqlDataSource
<asp:FormView
ID="fvStaff"
cssClass="person"
runat="server"
DataSourceID="srcPerson">
<InsertItemTemplate>
<!--DataSource srcRooms gets all rooms from room table in database. -->
<asp:SqlDataSource
ID="srcRoomsInsert"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SelectRoomsByBuildingID"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter
ControlID="ddlBuildingsInsert"
DefaultValue="1"
Name="ID"
PropertyName="SelectedValue"
Type="Int32"
/>
</SelectParameters>
</asp:SqlDataSource>
<asp:LabelcssClass="lblIdentifier" ID="lblLastName" runat="server"meta:resourcekey="LastName" />
<asp:TextBox ID="PersonLastName" runat="server" Text='<%#Bind("PersonLastName") %>'></asp:TextBox><br />
<asp:LabelcssClass="lblIdentifier" ID="lblFirstName" runat="server"meta:resourcekey="FirstName" />
<asp:TextBox ID="PersonFirstName" runat="server" Text='<%#Bind("PersonFirstName") %>'></asp:TextBox><br />
<asp:LabelCssClass="lblIdentifier" ID="lblBuilding" runat="server"meta:resourcekey="Building"></asp:Label>
<asp:DropDownList
ID="ddlBuildingsInsert"
cssClass="buildings"
runat="server"
DataSourceID="srcBuildings"
DataTextField="BuildingAbbreviation"
DataValueField="BuildingID"
AutoPostBack="True"
AppendDataBoundItems="true"
OnSelectedIndexChanged="ddlBuildingsInsert_SelectedIndexChanged">
<asp:ListItem enabled="true" value="0"meta:resourcekey="NoBuilding"></asp:ListItem>
</asp:DropDownList><br />
<asp:LabelcssClass="lblIdentifier" ID="lblRoom" runat="server"meta:resourcekey="Room" />
<asp:DropDownList
ID="ddlRoomsInsert"
cssClass="rooms"
runat="server"
Enabled="false"
DataSourceID="srcRoomsInsert"
DataTextField="RoomName"
DataValueField="RoomID"
AutoPostBack="true"
SelectedValue='<%# Bind("PersonRoomID") %>'>
</asp:DropDownList><br /
<asp:LabelcssClass="lblIdentifier" ID="lblPhone" runat="server"meta:resourcekey="Phone" />
<asp:TextBox cssClass="staff" ID="Phone" runat="server" Text='<%#Bind("PersonPhone") %>' /><br />
<asp:LabelcssClass="lblIdentifier" ID="lblPhonePrivate" runat="server"meta:resourcekey="Private" />
<asp:TextBox cssClass="staff" ID="PhonePrivate" runat="server"Text='<%# Bind("PersonPhonePrivate") %>' /><br />
<asp:LabelcssClass="lblIdentifier" ID="lblPhoneMobile" runat="server"meta:resourcekey="Mobile" />
<asp:TextBox cssClass="staff" ID="PhoneMobile" runat="server"Text='<%# Bind("PersonPhoneMobile") %>' /><br /
<asp:LabelcssClass="lblIdentifier" ID="lblEmail" runat="server"meta:resourcekey="Email" />
<asp:TextBox cssClass="staff" ID="Email" runat="server" Text='<%#Bind("PersonEmail") %>' /><br />
<asp:LinkButton cssClass="lnkButton" ID="InsertCancelButton"runat="server" CausesValidation="False" CommandName="Cancel"Text="<%$resources:translations,Cancel%>"></asp:LinkButton>
<asp:LinkButton cssClass="lnkButton" ID="InsertButton"runat="server" CausesValidation="True" CommandName="Insert"Text="<%$resources:translations,Insert%>"></asp:LinkButton>
</InsertItemTemplate>
</asp:FormView
This is what I tried in the first place, because the "Text='<%#Bind("PersonEmail") %>' " thing worked fine with the textboxes. Butafter selecting some value in the DropDownList ddlBuildingsInsert I getthe error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
pointing to the DropDownList ddlRoomsInsert. Deleting the"SelectedValue='<%# Bind("PersonRoomID") %>'" line from theDropDownList results in a missing insert parameter "PersonRoomID".Can you tell me the mistake I'm making here?
Thanks a lot, ToobThis one actually stumped me for a while too. The not so nice workaround I did for a similiar situation was to have a hidden value control, then during the dropdown change event I set the hidden value control to the selectedvalue from the dropdown, then link the insert sqldatasource to use the hidden value control instead. Bad, and requires a server postback for the dropdown change event, but for my particular app, I needed to do the postback anyhow.|||
in the data source control :
<SelectParameters>
<asp:ControlParameter Name="State" ControlID="statesDDL" PropertyName="SelectedValue" />
</SelectParameters>
Use a different bind method.
http://eramseur.blogspot.com/2005/11/dropdownlist-with-gridview.html
Hope that helps.
|||Thanks, the ControlParameter made it work. The next problem I wasfacing was the ControlID argument, it would only work if I used theddl's UniqueID. I'd like to put it in there somehow programmaticly(like <%# =statesDDL.UniqueID %>) but didn't find a way. Is therea way to do this?Thanks again,
Toob
Friday, March 9, 2012
problems with float numbers
hi
im using a stored procedure to insert float numbers in a data base...the parameter i send to the stored procedure is a float but every time i use it, it inserts a number with a lot of extra numbers i mean if i try to insert 5.3 it inserts 5.30000019073486 any idea why this is happening?
thak you
Himarcelocr2,
Have you done any sort of calculation before add/update or during you retrieve the float value?
Anyway, try this before you update your float value:
Math.Round(yourFloatValue, numberOfDecimal)
Hope this is help
|||What is the datatype of the column in the table that will have these values? If you only need 3 digits after the decimal use the decimal datatype and set the precision to 3. You can do this in the column properties under design view. Enterprise Mgr -> Right click on the table -> Design View.
|||hi
thank you to both of you
i tried with the math.round, but it converts the numbers to integers, and i need the numbers to be float numbers...
i cant change the type in the sqlserver because i need it to be a float number
is there a way to show only 3 or 4 numbers after the ","?
thank you
|||
Hello,
You should let is save like that only and should use round(SQL function-round(fieldname,4)) in Stored Procedures where you retrive data.
|||
Hi,
The Float datatype in both SQL Server and .NET framework is a floating point value. It is not accurate but an approximate value. This is defined by way that float is stored in memory.
However, if you need an accurate value, try to use Decimal instead in both SQL Server and .NET framework.
HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!