Using Session data in SQL SELECT parameters - c#

Okay, so I've got the follwing mark up:
Here I have a listbox that is to be populated by some SQL query.
<asp:ListBox ID="MyListBox" runat="server"
DataSourceID="MyDataSource" DataTextField="Field1" DataValueField="ID" ></asp:ListBox>
<asp:SqlDataSource ID="MyDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [ID], [Field1] FROM [Table1] WHERE ([ID2] = #ID2)">
<SelectParameters>
<asp:SessionParameter Name="ID2"
SessionField="ID2" DbType="Guid"/>
</SelectParameters>
</asp:SqlDataSource>
A few problems that I have:
The list box always is empty and when I try and test the query, it always presents a dialogue box asking for me to input "Type" "DbType" and "Value". I'm not too sure what to pick here.
I've tried running the application, ensuring that there is the needed session data in the correct key. (Session["ID2"] has been given a Guid). However, when I get to the listbox, it's empty even though I can look at the table data and know what should be there.

I have solved this issue. Basically, I assumed that the query should return results when it rightly shouldn't have done. Additionally, the data in Field1 was an empty string. So when it did return results, I wouldn't see anything in the ListBox.

Related

Error on dropdown with GridView when binding a SelectedValue from the database?

I have a GridView with multiple columns, the first column has a dropdown that allows the user to select a semester. When they select the semester it should save that selected value to the dropdown. There is a table that loads the dropdown, and then a table that loads the columns. My goal is to allow them to select a value from the dropdown, fill in the other columns, and save the results, so once they come back to the page selected value of the dropdown is shown along with the other data. There essenentially two tables, one for the dropdown for values to select, and another table that houses the actual value selected. When I enter in "Spring" into the main database table that houses the actual value, I get the below error. Any help is appreciated!
Code:
<asp:DropDownList ID="ddSemester" runat="server" DataSourceID="SemesterSrc" DataTextField="Semester" DataValueField="SemesterId" SelectedValue='<%#Bind("Semester") %>'>
<asp:SqlDataSource ID="SemesterSrc" runat="server" ConnectionString="<%$ ConnectionStrings:mainConnString %>" SelectCommand="SELECT * FROM [Semester]"></asp:SqlDataSource>
Error:
$exception {"'ddSemester' has a SelectedValue which is invalid because
it does not exist in the list of items. Parameter name:
value"} System.ArgumentOutOfRangeException
The value I have in the database is Spring, so Spring should show in the dropdown.
SelectedValue should be a single value in your database, such as "Spring" or "Summer". It should not be set to an entire column of data from your database. Right now, it's trying to bind to a whole column. This is only valid if your select statement returns one row, but I'm not even sure then if that's correct. Do you need selected value to be set on load? Normally, if you have multiple data controls where one control filters a second control, you need two data sources, where one has a filter set:
<asp:DropDownList ID="ddSemester" runat="server" DataSourceID="SemesterSrc" DataTextField="Semester" DataValueField="SemesterId">
<asp:SqlDataSource ID="SemesterSrc" runat="server" ConnectionString="<%$ ConnectionStrings:mainConnString %>" SelectCommand="SELECT DISTINCT Semester,SemesterId FROM [Semester]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SemesterSrcFiltered" runat="server" ConnectionString="<%$ ConnectionStrings:mainConnString %>" SelectCommand="SELECT * FROM [Semester]" FilterExpression="Semester='{0}'">
<FilterParameters>
<asp:ControlParameter Name="Semester" ControlId="ddSemester" PropertyName="SelectedValue"/>
</FilterParameters>
</asp:SqlDataSource>
The DDL will set a value that is used by the FilterParameters section of the SemesterSrcFiltered data source to filter out the data by just that value. You can then use that data source in other controls (like a grid view) to display more information. You should not set both controls to one data source and try to manipulate that data source on the fly with filtering without it unwantedly affecting your DDL too.

The server tag is not well formed asp.net C#

I'm doing the following query, and trying to use a code that come from a previous query.
But is giving me the following error: The server tag is not well formed.
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:TesteConnectionString %>" SelectCommand="SELECT * FROM [Equipa] where idAssemb=1 and idDept=<%# Eval("idDept") %>"></asp:SqlDataSource>
I'm using C# in Web forms asp.net
Can somebody help me on this?
Eval is used in DataBound controls to evaluate a field value in a row from the data source. You are trying to use it inside a Data Source control itself (SQLDataSource in this case). You should use parameterized query by specifying the value of parameter inside SelectParameters tag like this:-
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:TesteConnectionString %>"
SelectCommand="SELECT * FROM [Equipa] where idAssemb=1 AND idDept=#DeptId>
<SelectParameters>
<asp:ControlParameter ControlID="lblDeptId" Name="DeptId"
PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Please note, here I have shown the example of a control present inside your WebForm. You can specify where the value of DeptId is coming from via Cookie, QueryString, Form, Session etc.

C# ASP.NET SQL Data Source

How would I use a SQL Data Source to return the value from the select statement into a variable?
<asp:SqlDataSource ID="sdsProfile" runat="server" ConnectionString="<%$ ConnectionStrings:MYDB %>" SelectCommand="SELECT * FROM [UserProfileInformation] WHERE ([UserID] = #UserID)" >
<SelectParameters>
<asp:Parameter Direction="Output" Name="Verified" Type="Int32" />
</SelectParameters>
I have a colum that is called Verified in the database that is a bit. It is either a 0 or a 1. This tells me if the user profile has been verified by the admin. Is there a way to take the return value from the select statement and put it into the Verified parameter? This way I can access that parameter value in the code behind and manipulate the page based on a 0 or a 1? Thanks!
I am pretty sure you cannot do it without code-behind. At least not the way you can respect yourself the next day.

How dropdownlist bind with slqdatasource retrieve proper value?

Well I want to retrieve the value from the dropdown list to write for my .cs. The sqldatasource returns either Saving Account or Current account depending on what kind of account type does he/she have.
<asp:DropDownList ID="ddlPayBy2" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="accountType"
DataValueField="accountType">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Oakhorizons %>"
SelectCommand="SELECT [accountType] FROM [SavingAccount] WHERE ([custID] = #custID)">
<SelectParameters>
<asp:SessionParameter DefaultValue="OH00002" Name="custID"
SessionField="Session["custID"]" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Well it's been returning me nothing. I've tried this but nothing is returned. I always get the else statement and I tried to debug and it says SelectedValue = ""
if (ddlPayBy.SelectedValue == "Savings"){
account = "Savings";
}
else if (ddlPayBy.SelectedValue == "Current")
{
account = "Current";
}
else
{
Alert.Show("Please choose your account type properly");
}
i haven't used the sqldatasource object in awhile so forgive me, but are you missing something simple like not binding the control to the datasource object.
Perhaps your parameter isn't being set properly. Do you have access to SQL Profiler? Check and see if your query is running and running with the right query ...
also a side note ... this prob isn't it but your dropdown's id ="ddlPayBy2" but you're checking if (ddlPayBy.selected....

GridView DataKeyName for a DropDownList Select Statement

When the user selects a row to edit I have a dropdownlist as one of the controls. In order for me to populate that ddl I need one of the datakeyname values (there are three). I was guessing that I could retrieve this value when the OnEditing event fired and pass it to the select statement for the ddl. Just not sure how to do this. I am using a stored procedure to query the Database.
This is my sqldatasource for the ddl -
<asp:SqlDataSource ID="SqlDataSourceDebtor" runat="server"
ConnectionString="<%$ ConnectionStrings:AuditDevConnectionString2 %>"
SelectCommand="sp_fc_vm_getDebtorList" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="0" Name="ClientKey"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
The "ClientKey" is the datakeyname value I need.
There are multiple approach to do this, please check http://weblogs.asp.net/aghausman/archive/2009/01/08/get-primary-key-on-row-command-gridview.aspx
Let me know if you want anything specific other than this.
I did a post on this a while back here: http://peterkellner.net/2006/10/14/showallingridviewfromddl/

Categories