I have a product table that am updating. the fields updated are category_id, prod_name, prod_desc, prize, status. I am using a formview to do that updating. however i am having problems with the category_id. the category_id is a databound dropdownlist using a sqldatasource to fetch id and name from category table in the database. When a product has no category_id value, a null value is entered by default by sql.
The problem comes when i go to edititemtemplate mode in formview, the dropdownlist cannot show a null value from db so it throws an exception of:
'category_idDropdown' has a SelectedValue which is invalid because it
does not exist in the list of items. Parameter name: value
this is my code in the updateprod.aspx page:
<asp:DropDownList ID="category_idDropdown" runat="server"
AppendDataBoundItems="True"
AutoPostBack="True"
DataSourceID="catnames"
DataTextField="category_name"
DataValueField="category_id" ViewStateMode="Enabled"
SelectedValue='<%# Bind("category_id") %>' >
<asp:ListItem Text="-- Choose Category--" Value="0" Selected="True">
</asp:ListItem>
</asp:DropDownList>
this is my sqldatasource code the dropdownlist is databound to:
<asp:SqlDataSource ID="catnames" runat="server"
ConnectionString="<%$ ConnectionStrings:cloud_Kewl.Properties.Settings.conString %>"
SelectCommand="SELECT [category_id], [category_name] FROM [category]">
</asp:SqlDataSource>
& finally this is the sqldatasource used to update the product details:
<asp:SqlDataSource ID="productUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:cloud_Kewl.Properties.Settings.conString %>"
SelectCommand="SELECT prod_id, category_id, prod_name, prod_desc, prod_price, img_name, img_contenttype, prod_img, status FROM product WHERE (prod_id = #prod_id)"
UpdateCommand="UPDATE product SET category_id = #category_id, prod_name = #prod_name, prod_desc = #prod_desc, prod_price = #prod_price, status = #status WHERE (prod_id = #prod_id)">
<SelectParameters>
<asp:QueryStringParameter Name="prod_id" QueryStringField="prod_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="category_id" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="prod_name" Type="String" />
<asp:Parameter Name="prod_desc" Type="String" />
<asp:Parameter Name="prod_price" Type="Decimal" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="prod_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
i looked up in the questions regarding the formview and edititemtemplate in this site, but none came close to give me any idea on how to approach this problem.
I tried to solve it programatically but not success in cracking this problem. Please help me, i looked for the solution for 3 weeks. no success :(
Try rebinding your datasource prior to the null value being set:
protected void myFormView_OnItemEditing(object sender, ListViewEditEventArgs e)
{
DropDownList category_idDropdown = (DropDownList)(myFormView.FindControl("category_idDropDown"));
category_idDropdown.DataBind();
category_idDropdown.Items.Add(0, new ListItem("Select", null));
}
The problem likely isn't that you're trying to select a value that doesn't exist, but rather that you're trying to bind a DropDownList that already has a null value to a datasource that doesn't contain that value.
Related
I have a gridview populated with an sqldatasource. One of the columns of the gridview is a templatefield which contains a label as ItemTemplate, but in the EditItemTemplate I have a dropdownlist, instead of a textbox. I wanted to be able to edit a value from a certain column by selecting from a list of possible values.
So I created a second sqldatasource in order to extract the values from the database. I bounded the 2 sqldatasources. Visually it's ok. When I click on edit, the dropdownlist appears with the values that I want inside of it. When I select one of the values and click update, I get the error "
Input string was not in a correct format." I think I know why this is happening. Probably the values from the dropdownlist are type string and what I need is integer because that is the type for this column. I have tried setting the UpdateParameter as type Int32 but no luck. Is there any way to do this ?
This is the code I have used, but I have changed the column names with "Column_1", "Column_2", etc. In this example, "Column_6" is the templatefield with the dropdownlist in the edit.
<asp:GridView ID="DataUploadGridView" runat="server" AutoGenerateColumns="False" DataSourceID="First_DB" DataKeyNames="Column_3" >
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="Column_1" HeaderText="Column 1"/>
<asp:BoundField DataField="Column_2" HeaderText="Column 2"/>
<asp:BoundField DataField="Column_3" HeaderText="Column 3"/>
<asp:BoundField DataField="Column_4" HeaderText="Column 4"/>
<asp:BoundField DataField="Column_5" HeaderText="Column 5"/>
<asp:TemplateField HeaderText="Column 6">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Second_DB" DataTextField="Column_6_Description" DataValueField="Column_6_ID"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Column_6_ID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" HeaderText="Edit" />
<asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="First_DB" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" OnSelecting="First_DB_Selecting" OnSelected="First_DB_Selected" OnInserted="First_DB_Inserted" OnInserting="First_DB_Inserting" OnDeleting="First_DB_Deleting" OnUpdating="First_DB_Updating"
SelectCommand ="SELECT * FROM [tblFirst_DB]
UpdateCommand ="UPDATE [tblFirst_DB]
SET [Column_1] = #Column_1,
[Column_2] = #Column_2,
[Column_3] = #Column_3,
[Column_4] = #Column_4,
[Column_5] = #Column_5,
[Column_6] = #Column_6
WHERE [Column_3] = #Column_3"
DeleteCommand ="DELETE FROM [tblFirst_DB] WHERE [Column_3] = #Column_3"
<UpdateParameters>
<asp:Parameter Name="Column_1" Type="String" />
<asp:Parameter Name="Column_2" Type="String" />
<asp:Parameter Name="Column_3" Type="String" />
<asp:Parameter Name="Column_4" Type="String" />
<asp:Parameter Name="Column_5" Type="Int32" />
<asp:Parameter Name="Column_6" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="Column_3" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="Second_DB" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT [Column_6_ID], [Column_6_Description] FROM [tblSecond_DB]"></asp:SqlDataSource>
Hopefully you are still here. So just to recap the problem. I'm getting an "
Input string was not in a correct format. " error because I need the Column_6 parameter to be integer and the values are coming from a dropdownlist which is probably making each value a string.
What can I do so that the value for Column_6 stays int upon updating it ?
The only thing that comes to mind right now is to pass the parameter from the code behind somehow. When I select the dropdownlist value, I should trigger an OnSelectedIndexChanged event. I'm thinking here is where I should cast the Selected_ID to an integer and somehow feed it into the first sqldatasource. But the value would go into a label so that will turn it into a string again. So no use...I'm really stuck on this. Any help is welcome.
Thank you !
Update: I've noticed that if I remove all the UpdateParameters, except the one from Column_6, I'm not getting the error anymore. However the value is not going in the gridview. It's an empty cell. I looked in the DB and it's null.
I think it's null because in the UpdateParameters I now have:
<UpdateParameters>
<asp:Parameter Name="Column_6" Type="Int32" />
</UpdateParameters>
It's not getting the value from anywhere. It should get it from the Second_DB sqldatasource. I tried changing it to:
<UpdateParameters>
<asp:ControlParameter Name="Column_6" ControlID="Second_DB" Type="Int32" />
</UpdateParameters>
But it's not really working out like I expected. I feel like I'm getting closer but no idea why it's behaving like this.
I am learning ASP.NET(C#) and came across this code:
<asp:DropDownList
id="ddlMovieCategory"
DataSourceID="srcMovieCategories"
DataTextField="Name"
DataValueField="Id"
Runat="server" />
<asp:Button
id="btnSelect"
Text="Select"
Runat="server" />
<asp:GridView
id="grdMovies"
DataSourceID="srcMovies"
CssClass="gridView"
Runat="server" />
<asp:SqlDataSource
id="srcMovieCategories"
SelectCommand="SELECT Id, Name FROM MovieCategories"
ConnectionString="<%$ ConnectionStrings:Movies %>"
Runat="server" />
<asp:SqlDataSource
id="srcMovies"
SelectCommand="SELECT Title,Director FROM Movies
WHERE CategoryId=#Id"
ConnectionString="<%$ ConnectionStrings:Movies %>"
Runat="server">
<SelectParameters>
<asp:ControlParameter
Name="Id"
ControlID="ddlMovieCategory"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
I am wondering How the # symbol in the last control's SelectCommand work. It works as expected but I don't understand how it gets the value following the # i.e. if it is to use a variable which is escaped in ASP tag then how does it know that Id refers to the DropDownList's currently selected item.
Edit: I think I might have found the solution. I think it's from this bit of code, Am I correct:
<asp:ControlParameter
Name="Id"
ControlID="ddlMovieCategory"
PropertyName="SelectedValue" />
</SelectParameters>
The following string
"SELECT Title,Director FROM Movies WHERE CategoryId=#Id"
represents a parameterized sql query. The value of parameter Id, #Id, would be assigned to CategoryId, before this query is sent to database, in order to be executed.
Parameterized queries are the first thing we use, in order we avoid SQL injections.
Hi I want to bind text string in SelectCommand code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:finchsize_polibudaConnectionString %>"
SelectCommand="SELECT ocena FROM Oceny AS lista WHERE przedmiot = #ID"/>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="0" Name="ID"
PropertyName="Text" Type="String" />
</SelectParameters>
But my VS 2012 said that: 1.Validation(XHTML5): Element SelectParameters is not supported. 2. Element 'ControlParameter' is not a known element. Do yuo know how to fix that, or is it any other way to bind some text in SelectCommand ?
P.S. I tried
<asp:QueryStringParameter />
as well but it gives me exactly the same error.
The SelectParameters element needs to be a child of SqlDataSource. Right now you have it as a peer
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:finchsize_polibudaConnectionString %>"
SelectCommand="SELECT ocena FROM Oceny AS lista WHERE przedmiot = #ID">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="0" Name="ID"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
I have an SqlDataSource control with a selectcommand that doesn't fire onselected event (while another one, very similar, fires it).
The one that doesn't fire:
<asp:SqlDataSource ID="CommunicationSQLDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-WebApplication6-20131007103938ConnectionString1 %>"
SelectCommand="SELECT * FROM tCommunication" OnSelected="CommunicationSQLDataSource_Selected">
<SelectParameters>
<asp:Parameter Type="String" Name="ProjectID"></asp:Parameter>
</SelectParameters>
</asp:SqlDataSource>
(The one that fires, just for reference)
<asp:SqlDataSource ID="DetailsSQLDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-WebApplication6-20131007103938ConnectionString1 %>"
SelectCommand="SELECT [ProjectID], ..." OnSelected="DetailsSQLDataSource_Selected">
<SelectParameters>
<asp:Parameter Name="ProjectID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>
What I see is that the debugger doesn't reach the block of CommunicationSQLDataSource_Selected even though it is referenced in the control.
be sure you Use it in Form tag or asp:Content ..upon which Environment used
<form id="form1" runat="server">
<asp:SqlDataSource ID="DetailsSQLDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-WebApplication6-20131007103938ConnectionString1 %>"
SelectCommand="SELECT [ProjectID], ..." OnSelected="DetailsSQLDataSource_Selected">
<SelectParameters>
<asp:Parameter Name="ProjectID" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>
</form>
I solved it. I clicked config. data source, and walked through the wizard without changing anything. Thanks for all.
Set CancelSelectOnNullParameter property to false
I just got this message from Visual Studio 2005, when I clicked on the delete command I added to my gridview using the interface for configuring the grid:
Deleting is not supported by data
source 'transactionsSqlDataSource'
unless DeleteCommand is specified.
How can I activate that? I want my user to be able to delete a row when they click on the button Delete.
I already changed the property DeleteCommandType of my SQLDataSource to StoredProcedure.
What else do I need to do?
UPDATE
This is what I did, but I am missing something, I can't compile:
This is what I did at .cs:
public string DeleteCommand { get; set; }
How can I fix that, it says that the get is missing the body. I am not use to ASP.NET. Could you help me fixing this part of my code?
This is what I did at .aspx:
<asp:SqlDataSource ID="transactionsSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:membershipsDBConnectionString %>"
SelectCommand="SELECT [brukerId], [kjoptekvoten], [pengeneutbetalt], [fyringsolje], [biltype], [kjoptid] FROM [Informasjon]" DeleteCommandType="StoredProcedure"
DeleteCommand="DELETE FROM [Informasjon] WHERE fyringsolje='Kull: 2,42 kg';">
</asp:SqlDataSource>
Is the configuration for the datasource correct? Dont worry if you see that the delete command does not use parameters like the one above, it is just for a homework(I only need the query to pass).
UPDATE 2
I keep trying but i cant fix it. I am really newbie in ASP.NET and I am not very familiar with all the terminology. What exactly is happening here? How can I know the name of my store procedure?:
CS:
public string DeleteCommand { get { return DeleteCommand; } set { DeleteCommand = "";}
Aspx:
<asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:membershipsDBConnectionString %>"
DeleteCommandType="StoredProcedure" SelectCommand="SELECT [id], [kjoptekvoten], [pengeneutbetalt], [fyringsolje], [biltype], [kjoptid] FROM [Informasjon]"
DeleteCommand="StoredProcedureName">
</asp:SqlDataSource>
When I click on Delete that row needs to be erased from table Informasjon
DeleteCommand="<your stored proc>"
is needed in your SqlDataSource along with any parameters you want, for instance:
<DeleteParameters>
<asp:Parameter Name="myParameter" Type="Int32" />
</DeleteParameters>
which needs to be added in the SqlDatasource tag.
You can either call a stored procedure if you have created one like below :
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
// in the select command or your case delete you just input the name instead of a query
SelectCommand="SELECT * FROM [Table] WHERE [Column1]=#Parameter1 AND [Column2]=#Parameter2 AND [Column3]=#Parameter3;"
DeleteCommand="StoredProcedureName"
// Then ofcourse put the commandtype on storedprocedure but you already have that
DeleteCommandType="StoredProcedure">
// And you can declare the parameters like below
<SelectParameters>
<asp:Parameter Name="Parameter1" Type="String" />
<asp:Parameter Name="Parameter2" Type="Boolean" />
<asp:Parameter Name="Parameter3" Type="String" />
<asp:Parameter Name="Parameter4" Type="Boolean" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="Parameter1" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
For more information about storedprocedures please read : What is a stored procedure?
Or you can directly call a query like in the example below :
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [informasjon];"
DeleteCommand="DELETE FROM [informasjon] WHERE [id]=#id;">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int" />
</DeleteParameters>
</asp:SqlDataSource>
The connection string is something you will have to provide for yourself. For more info on the connectionstring please read : https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx