Calling Procedure in asp.net - asp.net

I want to call a Procedure to populate drop down in a form. Right now I am using the code like this:
<td class="style4">Choose Category: <font color="red">*</font></td>
<td class="style3">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="cat1"
DataTextField="catname" DataValueField="catname">
</asp:DropDownList>
<asp:SqlDataSource ID="cat1" runat="server" ConnectionString="<%$ ConnectionStrings:billingdatabase%>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [catname] FROM [Category]">
</asp:SqlDataSource>
</td>
The Procedure:
GO
CREATE PROCEDURE [dbo].[spshowproducts]
AS
BEGIN
SELECT catname FROM Category
END
GO

Sorry, I'm not sure I understand your question fully - some more details would be appreciated.
If your User Defined Function is a Stored Procedure you should be able to use that as the basis for the data. Bind your DropDownList to an SqlDataSource, and set the source for the SqlDataSource as the name of the Stored Procedure rather than specifying raw SQL.

Try this:
<td class="style4">Choose Category: <font color="red">*</font></td>
<td class="style3">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="cat1"
DataTextField="catname" DataValueField="catname">
</asp:DropDownList>
<asp:SqlDataSource ID="cat1" runat="server"
ConnectionString="<%$ ConnectionStrings:billingdatabase%>"
ProviderName="System.Data.SqlClient"
SelectCommand="EXEC [dbo].[spshowproducts]"></asp:SqlDataSource>
</td>

Related

Display multiple datas from same table from mysql using listview

I am using a listview to display datas from 3 tables in mysql.
My tables are,
theatredet(theaterid,theatername,locationid)
location(locationid,locationname)
screendet(screenname,theaterid,seatsavailable)
I want to display datas from screendet on the basis of theaterid from the table theatredet,i can only able to disply single data from screendet,there are multiple datas on that table with respect of theaterid.
My query is,
string query = "SELECT `theatredetails`.*,`Location`.`LocationName`,
`screendetails`.`ScreenName`,`screendetails`.`SeatsAvailable`
FROM `theatredetails`INNER JOIN `screendetails`
ON `screendetails`.`TheatreDetailsId` = `theatredetails`.`TheatreDetailsId`
INNER JOIN `location` ON `location`.`LocationId`=`theatredetails`.`LocationId`;";
My aspx
<asp:TextBox ID="TextBox6" runat="server" Text='<%#Bind("ScreenName") %>'></asp:TextBox>
<asp:TextBox ID="TextBox7" runat="server" Text='<%#Bind("SeatsAvailable") %>'></asp:TextBox>
Assuming you have already tested your query on MySql and it is returning intended number of rows.
You can populate your listview using the following code (I am using SqlDataSource to bind the ListView, you can use any other method)
<asp:ListView ID="lstviewScreens" runat="server" DataSourceID="ScreensSqlDataSource" >
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" >
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #E0FFFF;">
<td>
<strong>Theater Name:</strong><asp:Label ID="lblTheaterName" runat="server" Text='<%# Eval("theatername") %>' />
<br />
<strong>Screen Name:</strong><asp:Label ID="lblScreenName" runat="server" Text='<%# Eval("ScreenName") %>' />
<br />
<strong>Seats Available</strong><asp:Label ID="lblSeatsAvailable" runat="server" Text='<%# Eval("SeatsAvailable") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ScreensSqlDataSource" runat="server"
ConnectionString="Your connection String"
SelectCommand="SELECT theatredetails.*,Location.LocationName,
screendetails.ScreenName,screendetails.SeatsAvailable
FROM theatredetails INNER JOIN screendetails
ON screendetails.TheatreDetailsId = theatredetails.TheatreDetailsId
INNER JOIN location ON location.LocationId=theatredetails.LocationId">
</asp:SqlDataSource>
This will output your data in tabular format, however you can customize LayoutTemplate to change the generated html, even you can use GroupTemplate to group the data.

My query works in Access, but not in Visual Studio [duplicate]

This question already has an answer here:
Query returns no records to show on my webpage
(1 answer)
Closed 9 years ago.
I have a query that I have run in Access that doesn't seem to run when I execute it on my website site. Here's the code for the webpage that I want the database to populate content for:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/TravelJoansDB.accdb"
SelectCommand="SELECT * FROM [Table2] INNER JOIN BlogEntryItems ON Table2.ID=BlogEntryItems.BlogID AND Table2.ID=#ID">
<SelectParameters>
<asp:QueryStringParameter Name="ID" QueryStringField="Table2.ID" Type="Decimal" />
</SelectParameters>
</asp:AccessDataSource>
<asp:DataList ID="DataList1" DataSourceID="AccessDataSource1"
runat="server" ItemStyle-CssClass="picTableStyle"
DataKeyField="Table.2ID">
<ItemStyle></ItemStyle>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "PlaceImages/" + Eval("Image") %>' /><br />
<asp:Label ID="Label1" CssClass="picCaptionStyle" runat="server" Text='<%# Eval("Caption") %>' />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" class="picBodyText" runat="server" Text='<%# Eval("PicStory") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
When I take out the join, it works fine. I add the join and it doesn't give me any error or anything, it just seems as though there are no results for it to show on my website. Any ideas?
Note that I haven't worked on AccessDataSource. However, looking at the docs - here is what you should look into
Change the query to use ? instead of #ID
SelectCommand="SELECT * FROM [Table2] INNER JOIN BlogEntryItems ON Table2.ID=BlogEntryItems.BlogID AND Table2.ID= ?"
Because the AccessDataSource control extends the SqlDataSource class
and uses the System.Data.OleDb provider, you specify parameter
placeholders using the "?" placeholder character.
ref: http://msdn.microsoft.com/en-us/library/8e5545e1%28v=vs.100%29.aspx
If the above doesn't work, omit the data type (Decimal) of the parameter OR specify a relevant type such as Integer instead.

Delete row from a programmatically created table

In an asp.net listview I'm putting a table
<ItemTemplate>
<tr>
<td style="width:90%"><asp:TextBox Enabled="false" ID="txtOverall" Text='<%#Eval("Overall") %>' runat="server"></asp:TextBox></td>
<td style="width:10%"><asp:Button ID="cmdDelete" OnClick="DeleteRow" ToolTip='<%#Eval("tooltip") %>' class="alert tiny button" Text="x" runat="server" /></td>
</tr>
</ItemTemplate>
Which basically is a text string and next to it I want a button that will delete the row that the button appears on. The table holds more fields than this but for simplicity this the basic structure. All of the data is entered programmatically. Does anyone know how I can remove a single row from the table?
Thanks,
Craig
You need to handle the delete command
private void OnRecordDeleting(Object source, SqlDataSourceCommandEventArgs e) {
//your deleting code
}
and asp
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT * FROM Orders"
OnDeleting="OnRecordDeleting"
OnDeleted="OnRecordDeleted">
</asp:SqlDataSource>

display header of gridview even when results are null ASP.NET

I have a GridView and it works fine. But when I have no result the header disapears. Is there a way to show it without code-behind?
I am using 3.5
<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="Categorie"
DataValueField="Cat_ID"
AppendDataBoundItems="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Goed %>"
SelectCommand="SELECT * FROM [tbl_Cat]">
</asp:SqlDataSource>
update your sql query to return something in case of null.
if not exists (SELECT * FROM [tbl_Cat])
select ' ' as Categorie,' ' as catid
else
select SELECT * FROM [tbl_Cat]
will work on all framework verson
another option is to override databind method where you can check the datatable. if the table row count is 0 you can manually insert blank values and then databind.
Set the Below Property in gridview......
EmptyDataText="There are no crecords."
or set this template
<EmptyDataTemplate>
No data found!
</EmptyDataTemplate>
Set the ShowHeaderWhenEmpty property:
<asp:GridView runat="server" ShowHeaderWhenEmpty="true" ...
use property in gridview
ShowHeaderWhenEmpty="True"
You code with changes
<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="Categorie"
DataValueField="Cat_ID"
ShowHeaderWhenEmpty="True"
AppendDataBoundItems="True">
for 3.5 follow this link
http://www.aspdotnet-suresh.com/2010/12/v-behaviorurldefaultvmlo.html
For .net 3.5 use
<EmptyDataTemplate>
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
<tr>
<td colspan="2">No Data found..</td>
</tr>
</table>
</EmptyDataTemplate>
This would be the easiest way as far as I am concerned.

Markup getting erased in VS 2008

I am using master pages, update panel, AJAX Tab container and also modalpopup extenders and other AJAX validation controls. The line that is bold for Termination Reaon has a problem. I need to have <asp:ListItem Text="Select Contract Termination Reason.." Value=""></asp:ListItem> to control the error:
'ddlContractTerminationReason' has a SelectedValue which is invalid because it does not exist in the list of items"
It works but suddenly it started getting erased while trying to make some other changes to the application. If I try to type that again as soon as I click on Save, it gets erased and saves the file. My network administrator reinstalled everything for me assuming that some virus problem but looks like something else is causing this. Any help on this greatly appreciated.
<tr>
<td>
<asp:DropDownList ID="ddlContractTerminationReason" runat="server" AppendDataBoundItems="True" CausesValidation="True" CssClass="dropdown extralong" DataSourceID="dsTerminationReason" DataTextField="ReasonDescription" DataValueField="TerminationReasonID">
<asp:ListItem Text="Select Contract Termination Reason.."></asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="cusValTerminationReason" runat="server" ClientValidationFunction="RequireTerminationReason" ControlToValidate="ddlContractTerminationReason" ErrorMessage="Please enter Termination Reason OR Other Resson, if the Contractor is Terminated!" OnServerValidate="ServerValidateTerminationDate" SetFocusOnError="True" Text="*" ValidateEmptyText="True" ValidationGroup="ContrMasterGroup"></asp:CustomValidator>
<asp:SqlDataSource ID="dsTerminationReason" runat="server" ConnectionString="<%$ ConnectionStrings:Conn %>" SelectCommand="SELECT [TerminationReasonID], [ReasonDescription] FROM [TerminationReasons] ORDER BY [ReasonDescription]"> </asp:SqlDataSource>
</td>
<td class="rightalign">
<asp:Label ID="lblOtherContractTerminationReason" runat="server" CssClass="lbl" Text="if Others, Explain:"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtOtherContractTerminationReason" runat="server" CssClass="long" TextMode="MultiLine"></asp:TextBox>
</td>
<td> </td>
</tr>
After your DataBind execute this code
ddlContractTerminationReason.DataBind();
ddlContractTerminationReason.Items.Add("Select Contract Termination Reason..");

Resources