GridView and Stored Procedure? - asp.net

I want to use a GridView in Visual Studio. I went through the tutorial they have, and I have it set. However I don't see how I can use my Stored Procedure.
It sets me up with this:
<asp:SqlDataSource ID="SqlDataSourceViewRegistrants" runat="server"
ConnectionString="<%$ ConnectionStrings:Events2 %>"
SelectCommand="SELECT * FROM [Registrant]"></asp:SqlDataSource>
And I somehow want to replace that SelectCommand with a Stored Procedure, as such:
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmd.CommandText = "spGetAllRegistrants";
I'm not sure where to put this info of spGetAllRegistrants.

You need to change the SelectCommandType to StoredProcedure:
<asp:SqlDataSource ID="SqlDataSourceViewRegistrants" runat="server"
ConnectionString="<%$ ConnectionStrings:Events2 %>"
SelectCommand="spGetAllRegistrants"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>

Related

Using a different value from a drop down list

Sorry my title is crap but cant think of a better way to put it.
Using MS VS 2013 and MS SQL server 2012. VB
I have a drop down list and the stored procedure it calls gets two values from a table in SQL. The values are the ID and the name. The drop down list is displaying the name but I want to use the ID when a name is selected in the drop down list in code.
How do I do this?
My code for the DDL is
<asp:DropDownList ID="DDLAllTreatments" runat="server" DataSourceID="AllTreatments" DataTextField="Name" DataValueField="Name"></asp:DropDownList>
<asp:SqlDataSource ID="AllTreatments" runat="server" ConnectionString="<%$ ConnectionStrings:InfinitySPa %>" SelectCommand="SP_GetAllTreatments" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Change DataValueField to ID:
<asp:DropDownList ID="DDLAllTreatments" runat="server" DataSourceID="AllTreatments" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
<asp:SqlDataSource ID="AllTreatments" runat="server" ConnectionString="<%$ ConnectionStrings:InfinitySPa %>" SelectCommand="SP_GetAllTreatments" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

How can I bind a gridview to datasource with dynamic WHERE clause?

I am using Visual Basic for ASP. NET
I know how to populate a gridview with data using only code behind, like this
Dim ds as new datasourse
Dim da as new dataadaptor
Dim con as new SqlConnection
Dim cmd as new SqlCommand
...
Cmd = "select ticketID, problem_text from problems where support_engineer = " & Session("Logged_in_user_id")
...
Gridview1.Datasource = ds
Gridview1.Datasources. DataBind
And that works fines, but my question is: how can i drag and drop a gridview control, and using only the wizard to populate the gridview in design time, how can i define the SELECT statement to only select rows that are related to the logged in user id? See the sql statement above, it uses a session variable that i create in Page_Load event, but how can i use the same logic in design mode? (i don't want to modify the datasource control in code behind)
I looked in youtube and google, and this sites, but all results are simply showing me how to populate the gridview with all rows or with a static condition, not a dynamic one like the one i demonestrated.
Any help is highly appreciated
You could try replacing the form tag in the aspx file
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT ticketID, problem_text FROM Tabs WHERE (support_engineer = #Param1)">
<SelectParameters>
<asp:SessionParameter Name="Param1" SessionField="Logged_in_user_id" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
DataSourceID="SqlDataSource1">
</asp:GridView>
</div>
</form>
I found out the proper way of binding the gridview to a dynamic session variable, without involving code behind:. Simply use the property <SessionParameter> inside the <SelectParameter>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT ticketID, problem_text FROM problems
WHERE (support_engineer = #eng_id)
<SelectParameters>
<asp:SessionParameter
Name="eng_id"
SessionField="LoggedInUser"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
DataSourceID="SqlDataSource1">
</asp:GridView>

Concurrency check when updating using GridView

With this SqlDataSource, How can i be sure that the data I'm going to update is not modified in the meantime when i fetched data and when i'm about to make the update.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChartDatabaseConnectionString %>"
SelectCommand="SELECT * FROM [Student]"
UpdateCommand="UPDATE [Student] SET [StudentName] = #StudentName, [DOB] = #DOB, [Age] = #Age, [Course] = #Course, [City] = #City, [MobileNo] =
#MobileNo WHERE [StudentID] = #StudentID">
Is there any GridView method Or Any Parameter I can use ?
There is a propery on SqlDataSource named ConflictDetection. Setting that property to CompareAllValues may do the trick for you.

Query works in microsoft query in Excel but not in Visual Studio asp.net

Hello I have a query that combines two tables. It runs fine in excel however it times out in VS10 when trying to add it to a gridview. ODBC is ProvideX , it isn't very flexible... it won't even let me use a join but my code and query is below. Any questions let me know, Thank you!
SELECT CWIPH.CUST_NO, CWIPH.JOB_NAME, CJCMS.JOB_DESC
FROM CJCMS CJCMS, CWIPH CWIPH
WHERE CWIPH.JOB_NO = CJCMS.JOB_NO
asp
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Dsn=Jake" ProviderName="System.Data.Odbc"
SelectCommand="SELECT CWIPH.CUST_NO, CWIPH.JOB_NAME, CJCMS.JOB_DESC
FROM CJCMS CJCMS, CWIPH CWIPH
WHERE CWIPH.JOB_NO = CJCMS.JOB_NO">

Using SelectParameters in ASP.NET gives 'Must declare variable' errors

I have an ASP SqlDataSource connected to Sybase DB query, using Select Parameters that are populated by a dropdown:
Dropdown (works OK):
<asp:SqlDataSource ID="dsBondIDList" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [name], [bondID] FROM [bonds]">
</asp:SqlDataSource>
<asp:DropDownList ID="lstBondID" runat="server" DataSourceID="dsBondIDList" DataTextField="name" DataValueField="bondID">
SqlDataSource with parameter:
<asp:SqlDataSource ID="dsBonds" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = #bondID">
<SelectParameters>
<asp:ControlParameter Name="bondID" ControlID="lstBondID" PropertyName="SelectedValue" DefaultValue="-1" />
</SelectParameters>
</asp:SqlDataSource>
But I get an error when I try to run it:
ERROR [HY000] [DataDirect][ODBC Sybase Wire Protocol driver][SQL Server]Must declare variable '#bondID'.
Which of course I would get it I ran the sql literally as displayed. I expect the ASP engine to do the substitution before sending the query (and to use the default value of lstBondID if necessary)
Does anybody know why the #bondID is not being substituted with the lstBondID.SelectedValue?
Thanks in advance
Ryan
I just went through this problem 10 minutes ago and here's the fix:
If you need to keep using the ODBC driver you have, change your query to use ? instead of Named Parameters.
SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = ?
The parameters are used in the order they are added to the parameters collection (This gets hairy when you need to use the parameter more than once in your query etc).
Otherwise you can change drivers. Look for sybdrvodb.dll and regsvr32 it. Then setup your DSN and use that.

Resources