Delete row from a programmatically created table - asp.net

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>

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.

Value from a dropdown list isn't passing to the next page when I do a query string parameter

I'm trying to use the DataTextField property of a dropdown list to finish the link to the page it is supposed to navigate to. Here's the code:
page1.aspx
<table>
<tr>
<td>Search by last name: <asp:TextBox ID="searchTB" runat="server"/><br />
Search by first name: <asp:TextBox ID="FNameTB" runat="server"/><br />
</td>
<td style="padding-left: 10px;">
Search by Organization: <asp:DropDownList ID="OrgDDL" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb" SelectCommand="SELECT * FROM [ORG_SECTIONS]">
</asp:AccessDataSource>
</td>
</tr>
<tr>
<td style="text-align: center;"><asp:Button ID="Button1" runat="server" Text="Search" OnClick="Search" /></td>
<td style="padding-left: 10px; text-align: center;"><asp:Button ID="SearchbyOrgBtn" runat="server" Text="Search" OnClick="SearchByOrg" /></td>
</tr>
</table>
Code behind:
protected void Search(object sender, EventArgs e)
{
//TextBox LastName = (TextBox)this.FindControl("searchTB");
Response.Redirect("OrgsByName.aspx?LASTNAME=" + searchTB.Text);
}
protected void SearchByOrg(object sender, EventArgs e)
{
//TextBox LastName = (TextBox)this.FindControl("searchTB");
Response.Redirect("NamesByOrg.aspx?SectionName=" + OrgDDL.DataTextField);
}
NamesByOrg.aspx:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ORGANIZATIONS.ORG_NAME, ORG_SECTIONS.SectionName, ATTORNEYS.NAME, ATTORNEYS.LASTNAME, ATTORNEYS.EMAIL, ATTORNEYS.TEL
FROM (ORGANIZATIONS INNER JOIN (Org_Sec_Atty INNER JOIN ATTORNEYS ON Org_Sec_Atty.Atty_ID = ATTORNEYS.ATTY_ID) ON ORGANIZATIONS.ID = Org_Sec_Atty.OrgID) INNER JOIN ORG_SECTIONS ON Org_Sec_Atty.SecID = ORG_SECTIONS.ID
WHERE ORG_SECTIONS.SectionName LIKE #SectionName;">
<SelectParameters>
<asp:QueryStringParameter Name="SectionName" QueryStringField="SectionName" />
</SelectParameters>
</asp:AccessDataSource>
It doesn't matter what value I select in the DDL, it always tries to navigate to "NamesByOrg.aspx?SectionName=SectionName". When I had it as a textbox, it worked fine, but I'd like to keep it as a DDL for obvious reasons, lol.
The value of DataTextField is defined in the .aspx that you've provided, which happens to be "SectionName".
What you're looking for is something like SelectedValue or SelectedItem which will return whatever you selected in the drop down.

Calling Procedure in 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>

How can I add and delete row on Telerik's RadListBox Control?

I'm using Telerik's RadListBox control for my web application development, wondering how can I add row into RadListBox via TextBox control, below is my code fragment:
<telerik:RadListBox ID="rlbControl" runat="server" SelectionMode="Multiple">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' ></asp:Label>
</td>
<td style="width:20px"></td>
<td >
<asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age") %>' ></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadListBox>
Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
Age : <asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnDel" runat="server" Text="Delete" OnClick="btnDel_Click"/>
When Add button click, get input from two TextBox and bind into RadListBox.
For deleting, select row from RadListBox and click on Delete button, remove the selected row from the RadListBox.
My question is how can I add and delete rows?
Thank you in advanced.
Hi you can create datatable and datarows. then add the textbox values to dararow. then u can bind this datatable to the RadListBox. For deleting, u need to pick the ListItem index and delete the row from the datatable and again bind to the RadListBox. It will work..
Link for delete Row demo
Also check with my answer in the below link post. you will get idea about insert row
Add rows to ListView

Pass parameter id to sql query using url routing

I'm a beginner asp.net developer
Trying to create my first simple news portal pages.
Here is what I have:
Database
Admin_News.aspx to add news into database
Default.aspx to display all news with the title linkable
NewsDetails.aspx to display the details by ID when they click on the title in Default.aspx
How I did That:
in Default.aspx I used this code:
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
<tr>
<td>
<b>title</b>
</td>
<td>
<b>news</b>
</td>
<td>
<b>imges</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink runat="server" ID="hl" NavigateUrl='<%#"~/NewsDetails.aspx?id=" + Eval("id")%>' Text='<%# Eval("title") %>'></asp:HyperLink>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "news")%>
</td>
<td>
<img alt="" src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MoneyHolderConnectionString %>"
SelectCommand="SELECT [id], [title], [news], [imageurl], [detail] FROM [News]"></asp:SqlDataSource>
</div>
NewsDetails.aspx code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MoneyHolderConnectionString %>"
SelectCommand="SELECT [title], [news], [imageurl], [detail] FROM [News] WHERE ([id] = #id)">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<h1 align="right"><br />
<strong><p style="color:#000">
<%# DataBinder.Eval(Container.DataItem, "title")%>
</strong></h1><p/><br />
<br />
<p align="center"><img src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>'/>
<p/>
<br />
<br />
<p id="detail" align="right" style="font-size:25px"><%# DataBinder.Eval(Container.DataItem, "detail")%><p/>
<br />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
so what I'm doing is displaying the article according to the ID in .aspx?id= in the url.
everything was working fine.. until I used the url routing.
I changed/added some codes to change the structure of the url I don't want it to appear like ~/NewsDetails.aspx?id=1
I want to be like ~/News/1 instead with the same result.
So I have added this code to Global.asax:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("RouteForNews", "News/{id}", "~/NewsDetails.aspx");
}
and I added this code to the NewsDetails.aspx.cs code behind:
string id = Page.RouteData.Values["id"].ToString();
and I changed the NavigateUrl in NewsDetails.aspx to:
NavigateUrl='<%#"~/News/" + Eval("id")%>'
now when I open Default.aspx the news appear with the titles linkable to ~/News/"id number", when i click on a title, NewsDetails.aspx opens with the link of /News/idnumber but no data inside..its empty i only can see the design of the master page.
I would appreciate any help from you what should I do to display the news, the id value in the url it goes to string id variable but I don't know how to pass it to the sql query (i'm not sure if this is the problem)
You should not use the asp:QueryStringParameter in the asp:SqlDataSource in the NewsDetails page, as your parameter is no longer in the QueryString. A QueryString is the part of URL that comes after the question mark.
Use this link to know how to pass parameters to select statement:
How to pass variable to SelectCommand of SqlDataSource?
connectionstring="<%$ ConnectionStrings:MyConnection %>"
selectcommand="SELECT Name, Age FROM ContactInfo where
State=#StateCode">

Resources