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>
Related
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.
First off, I am VERY NEW to C#. I have been tasked with this project for a business unit at work.
I have a userform to retrieve some data from an SQL DB, and then allow user input into text boxes. Then when they hit submit, I need the received data from the DB and the User to be written into the DB on a new table. It's written in C# and Asp.net.
On page load I have it gathering data from the DB:
HTML Markup:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:contr1 %>"
SelectCommand="SELECT DISTINCT [Employee Name] FROM Total_Tech_Minutes WHERE
(Site = 'US.TN.MEMPHIS') ORDER BY [Employee Name]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:contr1 %>"
SelectCommand="SELECT [Total Minutes] AS Total_Minutes FROM [Total_Tech_Minutes]
WHERE ([Employee Name] = #Employee_Name)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Employee_Name"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
That works perfectly. I even see the action on the DB Profiler.
The problem is when I do the rest of the code. I see nothing on the DB Profiler for the code below.
The following is the html markup for inserting it all back into the db:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:contr1 %>"
InsertCommand="Insert into OBH([Tech Name],[Total Minutes],[Off Bench Time],
[Submitted By],Comments)
Select [Tech Name]=TechName,[Total Minutes]=#TM,[Off Bench Time]=#OBT,[Submitted By]=#SB,
Comments=#Comments"
ProviderName="<%$ ConnectionStrings:contr1.ProviderName %>"
CancelSelectOnNullParameter="False" DataSourceMode="DataReader">
<InsertParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="TechName" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DataList1" Name="TM" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="TextBox1" Name="OBT" PropertyName="Text" />
<asp:FormParameter FormField="Login1.UserName" Name="SB" />
<asp:ControlParameter ControlID="TextBox2" Name="Comments" PropertyName="Text" />
/InsertParameters>
/asp:SqlDataSource>
This is the Button action:
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource3.DataBind();
}
#MMK This works perfectly.
protected void Button1_Click(object source, EventArgs e) { SqlDataSource3.Insert(); }
– MMK yesterday
just use sql query and hit
insert into table2(val1,val2,val3.val4....) select isnull(a,''),isnull(b,''),isnull(c,''),isnull(d,'')...
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
well I've stumbled upon similar cases of this problem, and also followed the solution approaches. But my case is quite strange, as I have 2 different DetailsView-controls (with different data), one works, the other doesn't.
So here's the problem in detail. I get the following error message:
DropDownList2 has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I am aware of this thread, and maybe I'm stupid and not seeing something. But maybe you do. I have 2 Detailsviews, which bind their data based on a dataset for one user. Both DVs have DropdownList-Controls inside their EditItemTemplates, which bind the possible values for this very column. I use SelectedValue='<%# Bind("mycolumn") %>' for my DropDownList-template exactly the same way in the 2 DVs.
As said, I am aware of code-behind workarounds, but I want to avoid those, to keep my code clean and consistent. I can't really document why I use a workaround on one DetailsView, and why I don't on the other.
Here is the code of my 2 DetailsViews:
<asp:DetailsView ID="dv_theme_ava" runat="server" Height="50px" Width="125px" AutoGenerateRows="False"
DataSourceID="SqlDataSource1" DefaultMode="Edit" AutoGenerateEditButton="True" DataKeyNames="Pat_ID">
<Fields>
<asp:TemplateField HeaderText="Theme">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="theme" DataValueField="theme"
SelectedValue='<%# Bind("theme") %>'>
</asp:DropDownList>
<asp:Label ID="lolbel2" runat="server" Text='<%# Bind("theme") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [theme] FROM [gui_themes]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Avatar">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3"
DataTextField="avatar" DataValueField="avatar">
</asp:DropDownList>
<asp:Label ID="lolbel" runat="server" Text='<%# Bind("avatar") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [avatar] FROM [gui_avatars]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [pat_gui_config] WHERE ([Pat_ID] = #Pat_ID)" DeleteCommand="DELETE FROM [pat_gui_config] WHERE [Pat_ID] = #Pat_ID"
InsertCommand="INSERT INTO [pat_gui_config] ([Pat_ID], [theme], [avatar]) VALUES (#Pat_ID, #theme, #avatar)"
UpdateCommand="UPDATE [pat_gui_config] SET [theme] = #theme, [avatar] = #avatar WHERE [Pat_ID] = #Pat_ID">
<DeleteParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="Pat_ID" SessionField="sel_pat_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
<asp:Parameter Name="Pat_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="dv_treat_edit" runat="server" AutoGenerateEditButton="True"
AutoGenerateRows="False" DataKeyNames="Tr_ID" DataSourceID="sql_newcat" DefaultMode="Edit"
Height="50px" Width="90%" AllowPaging="True" CssClass="dv_details" Style="margin: 0 auto;">
<Fields>
<asp:BoundField DataField="Tr_ID" HeaderText="Tr_ID" InsertVisible="False" ReadOnly="True"
SortExpression="Tr_ID" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:BoundField DataField="syn_ger" HeaderText="Display Name (German)" SortExpression="syn_ger" />
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:DropDownList ID="ddl_type0" runat="server" DataSourceID="sql_ddl_type0" DataTextField="type"
DataValueField="type" SelectedValue='<%# Bind("type") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_type0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [type] FROM [entry_type]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exclusive for Patient_ID">
<EditItemTemplate>
<asp:DropDownList ID="ddl_excl_pat0" runat="server" DataSourceID="sql_ddl_exclpat0"
DataTextField="Pat_ID" DataValueField="Pat_ID" SelectedValue='<%# Bind("custom_cat_for_Pat") %>'
AppendDataBoundItems="true">
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_exclpat0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [Pat_ID] FROM [patients]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
<CommandRowStyle CssClass="dv_footer" />
<RowStyle CssClass="dv_tr" />
<PagerSettings Mode="NumericFirstLast" Position="Top" Visible="False" />
</asp:DetailsView>
<asp:SqlDataSource ID="sql_newcat" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [treat_cat]" DeleteCommand="DELETE FROM [treat_cat] WHERE [Tr_ID] = #Tr_ID"
InsertCommand="INSERT INTO [treat_cat] ([description], [syn_ger], [type], [custom_cat_for_Pat]) VALUES (#description, #syn_ger, #type, #custom_cat_for_Pat)"
UpdateCommand="UPDATE [treat_cat] SET [description] = #description, [syn_ger] = #syn_ger, [type] = #type, [custom_cat_for_Pat] = #custom_cat_for_Pat WHERE [Tr_ID] = #Tr_ID">
<DeleteParameters>
<asp:Parameter Name="Tr_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
<asp:Parameter Name="Tr_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Note: the data model is quite simple. For comparison I used the 2 fields "theme" and "type". Both tables only have 1 column in my database, holding string entries.
Now, the "type"-DDL gets it's items perfectly fine, and has the SelectedValue bound to the value brought to the DetailsView by the datasource. When I bind the "theme"-DDL with SelectedValue, I get the error. Interesting: in the same EditItemTemplate I have set up a Label (ID "lolbel2" :p) to check the databinding. It works (of course, when I remove the SelectedValue from the DDL). So without the SelectedValue in the DDL my output is like
[DROPDOWNLIST] with items "space", "magic"
[LABEL] with text "magic" (since this is the value of my test-user).
Am I missing something? Am I completely nuts?
So, sorry for re-asking this question for like the 10th-time, but I want to UNDERSTAND what my code does.
Thanks in advance!
Konrad
Ooooohkay. Found the problem, that's what you get when you are stubborn as a goat. :)
When debugging a workaround with a HiddenField I noticed that the value, which gets bound the same way as the Label-Control, has some trailing whitespace. In particular: instead of "dog" I got "dog ". While this isn't shown in the asp:Label, I guess this was the reason why the value wasn't found in the DropDownList.
Where did the whitespace come from? In my SQL table I created the columns for "theme" and avatar as "nchar", and not "nvarchar". Apparently when using "nchar" as DataType the not-used chars of the tupels are filled with whitespace, or let's say the fields have fixed width (always x chars).
Changing the datatype to "nvarchar" helped me to get rid of the whitespace, and now the databinding of the DDLs works just fine.
I am documenting this, as maybe someone else will stumble over this aswell - and since there are 50 solutions and workarounds, maybe just taking a look at the database sometimes does the trick.
I have a SqlDataSource SelectCommand on my .aspx page.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT * FROM [UserResult]"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
This statement will grep a list of data from the table UserResult.
However I would only want to list specific results which belongs to the user.
I have attached a column in the UserResult table which is username, and I tried to form an sql statement like this but it seems to search for user User.Identity.nAME :
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SODConnectionString %>"
SelectCommand="SELECT * FROM [UserResult] WHERE [username] LIKE 'User.Identity.Name'";"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
May I know how can I do it?
Update:
From solution from Curt, I have tried to implement the following:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SODConnectionString %>"
SelectCommand="SELECT * FROM [UserResult] WHERE [username]=#username"
OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter Name="username" Type="String" DefaultValue='<%=User.Identity.Name %>' />
</SelectParameters>
</asp:SqlDataSource>
However I am still not able to retrieve data according to the username.
I tried to hard code a username into the username parameter and it worked:
Anyone could help?
<asp:Parameter Name="username" Type="String" DefaultValue="james" />
Your statement is looking for User.Identity.Name because this is set as a string.
Try using code blocks:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SODConnectionString %>"
SelectCommand="SELECT * FROM [UserResult] WHERE [username]=#username"
OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter Name="username" Type="String" DefaultValue='<%=User.Identity.Name %>' />
</SelectParameters>
</asp:SqlDataSource>
Embedded Code Blocks in ASP.NET Web Pages
It is not necessary to declare a Default Value in .aspx file. You can only add to the code behind file.
protected void Page_Init(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["UserName"].DefaultValue = User.Identity.Name;
}