I need to replace the ? with lblDates.Text,how is that possible?? lblDates is a label to which I saved a date(string) from calendar,so I need this so I can fill the gridview
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_data/Baza1.accdb"
SelectCommand="SELECT [Spol], [Prezime], [Vrijeme], [Stol] FROM [rezervacije] WHERE ([Datum] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="lblDates" Name="Datum" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:AccessDataSource
Since the ControlParameter that retrieves lblDates.Text has Name="Datum", replace ? in SelectCommand with #Datum:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_data/Baza1.accdb"
SelectCommand="SELECT [Spol], [Prezime], [Vrijeme], [Stol] FROM [rezervacije] WHERE ([Datum] = #Datum)">
<SelectParameters>
<asp:ControlParameter ControlID="lblDates" Name="Datum" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:AccessDataSource>
For more information, see Using Parameters with Data Source Controls.
Related
I'm trying set default value of select parameter in MVC:
<asp:Parameter Name="PickDate" Type="DateTime" DefaultValue="<%: DateTime.Now.ToString("yyyy-MM-dd")%>" />
But it doesn't work... I got error":
String was not recognized as a valid DateTime.
Can I set DateTime.Now to default value?
Edit1: Still doesn't work...
<asp:SqlDataSource ID="SqlDataSourceDailyPickingList" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM DailyPickingList where PickDate = #PickDate">
<SelectParameters>
<asp:Parameter Name="PickDate" Type="DateTime" DefaultValue="<%: DateTime.Now%>" />
</SelectParameters>
</asp:SqlDataSource>
Is it at all possible ?
You are casting to string the default value when explicitly setting the type as DateTime. Try this:
<asp:Parameter Name="PickDate" Type="DateTime" DefaultValue="<%: DateTime.Now%>" />
Or this
<asp:Parameter Name="PickDate" Type="string" DefaultValue='<%: DateTime.Now.ToString("yyyy-MM-dd")%>' />
I am trying to limit the selection of an INSERT using a DropDownList, my TextBox all work correctly but I cannot seem to figure out how to use a DropDownList in a similar fashion.
Here is my current code-behind:
private void _subInsert(RepeaterCommandEventArgs e)
{
TextBox txtPostTitle = (TextBox)e.Item.FindControl("txt_post_titleI"); // Find the control with the information you wish to store
TextBox txtPostBody = (TextBox)e.Item.FindControl("txt_post_bodyI");
TextBox txtPostAuthor = (TextBox)e.Item.FindControl("txt_post_authorI");
DropDownList ddlPostType = (DropDownList)e.Item.FindControl("ddl_post_typeI");
sds_main.InsertParameters["post_title"].DefaultValue = txtPostTitle.Text; // Insert information into this location in the DB
sds_main.InsertParameters["post_body"].DefaultValue = txtPostBody.Text;
sds_main.InsertParameters["post_author"].DefaultValue = txtPostAuthor.Text;
sds_main.InsertParameters["post_type"].DefaultValue = ddlPostType.SelectedValue.ToString();
sds_main.Insert(); // Perform Insert
}
The TextBoxes all insert correctly, but the DropDownList does not allow me to use the SelectedValue in the Insert. I am getting a "Object reference not set to an instance of an object." error.
This is the code for my SQLDataSource control on the presentation page:
<asp:SqlDataSource
id="sds_main"
runat="server"
ConnectionString="<%$ ConnectionStrings:fntn0070DBConnectionString %>"
SelectCommand="SELECT * FROM [blogTest]"
DeleteCommand="DELETE FROM [blogTest] WHERE [id] = #id"
InsertCommand="INSERT INTO [blogTest] ([post_title], [post_body], [post_author], [post_type]) VALUES (#post_title, #post_body, #post_author, #post_type)"
UpdateCommand="UPDATE [blogTest] SET [post_title] = #post_title, [post_body] = #post_body, [post_author] = #post_author, [post_type] = #post_type WHERE [id] = #id" >
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="post_title" Type="String" />
<asp:Parameter Name="post_body" Type="String" />
<asp:Parameter Name="post_date" Type="String" />
<asp:Parameter Name="post_author" Type="String" />
<asp:Parameter Name="post_type" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="post_title" Type="String" />
<asp:Parameter Name="post_body" Type="String" />
<asp:Parameter Name="post_author" Type="String" />
<asp:Parameter Name="post_type" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
And this is my DDL on the presentation page:
<asp:DropDownList ID="ddl_postTypeI" runat="server">
<asp:ListItem>Web Development</asp:ListItem>
<asp:ListItem>Photography</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
I have tried my best to search for this problem on Google and StackOverflow but have only be able to find information on how to bind data to the DDL.
I am very new to ASP.NET 4, just starting to learn it in my web development course so please ELI5 if you are able to answer. Thanks in advance for any help you are able to provide.
you can not use it directly without giving value of dropdown list.
try this
<asp:DropDownList ID="ddl_postTypeI" runat="server">
<asp:ListItem Value="Web Development">Web Development</asp:ListItem>
<asp:ListItem Value="Photography">Photography</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
and then try to insert in data , if you are passing integer value then use value="1" otherwise it will throw an error.
Thanks,
I have a textbox which stores phone number.
<asp:TextBox runat="server" ID="phone" MaxLength="10" Columns="10" Width="90px" />
<asp:MaskedEditExtender ID="phone_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" TargetControlID="phone" Mask="(999)-999-9999" ClearMaskOnLostFocus="False">
</asp:MaskedEditExtender>
The question is when I insert phone number 9999999999, the number in the database becomes
(999)-9999.
What I want is 9999999999.
The other code is:
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" ConnectionString="<%$ ConnectionStrings:MembershipDB %>"
InsertCommand="CreateAdditionalAccountInfo" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:ControlParameter Name="FirstName" Type="String" ControlID="FirstName" PropertyName="Text" />
<asp:ControlParameter Name="LastName" Type="String" ControlID="LastName" PropertyName="Text" />
<asp:ControlParameter Name="Degree" Type="String" ControlID="Degree" PropertyName="Text" />
<asp:ControlParameter Name="Organization" Type="String" ControlID="Organization"
PropertyName="Text" />
<asp:ControlParameter Name="Phone" Type="String" ControlID="Phone" PropertyName="Text" />
<asp:ControlParameter Name="Ext" Type="String" ControlID="Ext" PropertyName="Text" />
<asp:ControlParameter Name="last4SSN" Type="String" ControlID="TextSSN" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
The stored Procedure query is
INSERT INTO [User_ExtraInfo] ([UserId], [FirstName], [LastName], [Degree], [Organization], [Phone], [Ext], [last4SSN],[Trained]) VALUES (#UserId, #FirstName, #LastName, #Degree, #Organization, #Phone, #Ext
Thanks.
Change the proc to this:
INSERT INTO [User_ExtraInfo] ([UserId], [FirstName], [LastName], [Degree], [Organization], [Phone], [Ext], [last4SSN],[Trained]) VALUES (#UserId
, #FirstName
, #LastName
, #Degree
, #Organization
, REPLACE(REPLACE( REPLACE(#Phone,'(',''),')','') ,'-','') , #Ext )
The reason I suggest this approach is because you are using a SQLDatasource object and I don't think it's possible to instruct the insert parameter -at least not declaratively from the markup- to ignore the mask. Your best bet is probably just to modify the proc.
UPDATE - programmatic approach:
Add a handler for OnInserting to the SqlDataSource:
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" OnInserting="On_Inserting"
Handle the event on code and do the replacement there:
protected void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e) {
e.Command.Parameters["#Phone"].Value=phone.Text.Replace("..."); // etc
}
Note: not tested but it's the idea.
I have an asp.net application in which I am inserting into an sql table using a GridView linked to an SqlDataSource. The problem is that I do not know the 'Identity' parameter to use on insertion. The 'Identity' column should be automatically incrementing, however upon inserting, I get an exception that the Identity param must not be null. How do I either craft the insert statement so that this works or how do I get the next correct identity # to specify? Thanks.
Here is my SqlDataSource code:
<asp:SqlDataSource ID="VehiclesSqlDS" runat="server"
ConnectionString="<%$ ConnectionStrings:RamRideOpsConnectionString %>"
SelectCommand="SELECT * FROM [Vehicles]"
ConflictDetection="CompareAllValues"
InsertCommand="INSERT INTO [Vehicles] ([Identity], [CarNum], [MaxPassengers], [Status], [CurrPassengers], [StartAdd], [EndAdd], [AvgRideTime], [numRides]) VALUES (#Identity, #CarNum, #MaxPassengers, #Status, #CurrPassengers, #StartAdd, #EndAdd, #AvgRideTime, #numRides)"
OnInserting="VehiclesSqlDS_Insert"
OldValuesParameterFormatString="original_{0}">
<InsertParameters>
<asp:Parameter Name="Identity" Type="Int32 "/>
<asp:Parameter Name="CarNum" Type="Int32" DefaultValue="-1"/>
<asp:Parameter Name="MaxPassengers" Type="Int32" DefaultValue="3" />
<asp:Parameter Name="Status" Type="String" DefaultValue="Ready" />
<asp:Parameter Name="CurrPassengers" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="StartAdd" Type="String" />
<asp:Parameter Name="EndAdd" Type="String" />
<asp:Parameter DbType="Time" Name="AvgRideTime" />
<asp:Parameter Name="numRides" Type="Int32" DefaultValue="0" />
</InsertParameters>
</asp:SqlDataSource>
you need to set parameter direction to OUTPut, to get the identity value. from MSDN
and handle the onInserted event to get, the identity value
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
string sID = e.Command.Parameters["#Identity"].Value.ToString();
//Display new ID
}
you can try like this.
<asp:sqlDataSource ID="Datasource"
SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = #EmpID"
InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (#LastName, #FirstName);
SELECT #EmpID = SCOPE_IDENTITY()"
UpdateCommand="UPDATE Employees SET LastName=#LastName, FirstName=#FirstName
WHERE EmployeeID=#EmployeeID"
DeleteCommand="DELETE Employees WHERE EmployeeID=#EmployeeID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
</InsertParameters>
</asp:sqlDataSource>
You should omit the Identity column from the insert statement. Only include the data columns. The Sql Server will create the auto-increment then.
INSERT INTO [Vehicles] ([CarNum], [MaxPassengers], [Status], [CurrPassengers], [StartAdd], [EndAdd], [AvgRideTime], [numRides]) VALUES (#CarNum, #MaxPassengers, #Status, #CurrPassengers, #StartAdd, #EndAdd, #AvgRideTime, #numRides)
Ever Present, that is assuming that Identity is an auto-incremeneted column. It could just be a regular column.
You need to somehow retrieve the latest Identity first before trying to insert.
Alternatively you could write a trigger on the database that automatically updates the Identity column when a new item is added.
These snippets may help:
<InsertParameters>
<asp:Parameter Name="SalesRepID" Type="Int32" />
...more params
<asp:Parameter Name="QuoteID" Type="Int32" Direction="Output" />
</InsertParameters>
Note the second command added
InsertCommand="INSERT INTO [MyTable] (fields); SET #QuoteID = Scope_Identity();"
This doesn't work. I've got an exception from SQL databse that column does not allow nulls.
<asp:SqlDataSource ID="MyDataSOurcet" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
InsertCommand="INSERT INTO MyTable(Name) VALUES (#Name) WHERE NameID = 1"
<InsertParameters>
<asp:Parameter Name="Name" Type="String" DefaultValue=""/>
</InsertParameters>
</asp:SqlDataSource>
You could try
<asp:Parameter Name="Name" Type="string" DefaultValue=""
ConvertEmptyStringToNull="false" />
But I thought that was default.
try this:
<asp:SqlDataSource ID="MyDataSOurcet" runat="server" OnInserting="MyDataSOurcetInserting" ... >
//cs
protected void MyDataSOurcetInserting(object sender, SqlDataSourceCommandEventArgs e)
{
//check if nothing has been inserted then
e.Command.Parameters["Name"].value = DBNull.Value;
}
Good luck!