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")%>' />
Related
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.
What I'm trying to do is to insert a username, and their monthly hour limit to my SQL Server database. I've used the automatically generated statements for updating and deleting. I just need to add in new users now. The code below, should work as far as I know, but it doesn't. I think it's the way I've written it.
The part in comments is what the Userdata.aspx file automatically generated, so I'm trying to convert it to use my 2 text boxes.
Thanks a lot.
protected void Button1_Click1(object sender, EventArgs e)
{
string sql = "INSERT INTO [UserData]([UserName], [MonthlyHourLimit]) VALUES ("+ TextBox1.Text + "," + TextBox2.Text + ")";
//INSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (#UserName, #MonthlyHourLimit)"
SqlDataSource1.InsertCommand = sql;
GridView1.DataBind();
}
You need to configure your data source to use parameters.
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="select [UserName], [MonthlyHourLimit] from [UserData] where UserName= #UserName"
InsertCommand="IINSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (#UserName, #MonthlyHourLimit);"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="UserName" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="UserName" Direction="Input" Type="String" />
<asp:Parameter Name="MonthlyHourLimit" Direction="Input" Type="String" />
</InsertParameters>
</asp:sqlDataSource>
UPDATE:I've forgot to mention, you would like to use ControlParameter and not simple Parameter. Take a look at following snippet:
<asp:СontrolParameter Name="UserName" ControlId="ddlUserNames" PropertyName="SelectedValue"/>
...
<asp:DropdownList
ID="ddlUserNames"
runat="server"
Autopostback="True">
<asp:Listitem Selected="True">Users</asp:Listitem>
<asp:Listitem Value="Peter">Peter</asp:Listitem>
<asp:Listitem Value="Jessica">Jessica</asp:Listitem>
</asp:Dropdownlist>
Take a look at corresponding MSDN page describing usage of SqlDataSource in details.
UPDATED 2: complete example in order to avoid confusion
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="select [UserName], [MonthlyHourLimit] from [UserData] where UserName= #UserName"
InsertCommand="IINSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (#UserName, #MonthlyHourLimit);"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="UserName" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter Name="UserName" ControlId="txtUserName" Direction="Input" Type="String" />
<asp:ControlParameter Name="MonthlyHourLimit" ControlId="txtMonthlyHourLimit" Direction="Input" Type="String" />
</InsertParameters>
</asp:sqlDataSource>
<asp:TextBox runat="server" ID="txtUserName" />
<asp:TextBox runat="server" ID="txtMonthlyHourLimit" />
Datasource.InsertCommand is a property.
Datasource.Insert() is a method.
You should also use parameters.
datasource.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text
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 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!