ASP access database dynamic output - asp.net

<%# Page Language="VB" AutoEventWireup="false
CodeFile="Customers.aspx.vb"
Inherits="Customers" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" Text="By Contact Name: " /> <asp:TextBox ID="Textb"
runat="server" ></asp:TextBox>
<asp:DataGrid runat="server" ID="out" DataSourceID="AccessDataSource1" />
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/northwind.mdb"
SelectCommand="SELECT * FROM [Customers] WHERE ([ContactName] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="Textb" Name="ContactName"
PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:AccessDataSource>
<asp:Label ID="Label2" runat="server" Text="By Company Name: " /> <asp:TextBox
ID="Texta" runat="server" ></asp:TextBox>
<br />
<asp:DataGrid runat="server" ID="out0" DataSourceID="AccessDataSource2" />
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/northwind.mdb"
SelectCommand="SELECT * FROM [Customers] WHERE ([CompanyName] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="Texta"
Name="CompanyName" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
Guys, I am trying to create kind of ... dynamic output meaning: I have a database and I want to search it and display the output in the format above. The thing is:
1. When I do not have the second textbox and dataGrid I get output by the search I do but I still need to hit enter. How do I make it so it automatically displays result while i am writing the search itself. I have in mind something like onchangedtext event if you get me.
2. When I add the datagrid I do not get any output even if I hit enter on the first(Name search). How do I fix that?
Thanks

You must write javascript code, in order to ensure that you don't postback data, because it's very expensive.
For disable just add style hidden on cells.
I suggest this post (Similar case with color) : http://csharp-guide.blogspot.fr/2012/07/aspnet-jquery-gridview-filter.html

Related

What is wrong with this ASP.net DataBinding program?

I am used to writing the data on the client side, then with JavaScript, validate and submit forms to a new page on server side which updated the database and then go again.
I am thinking with programming on ASP.net, it is not a good practice to do it on client side because security is better on server side.
I am trying to do this using sqlDataSources and DataBinding unsuccessfully.
On my page I have only two input boxes. These boxes are empty.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Prueba.aspx.cs"
Inherits="MaximaBR.com.Prueba" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource id="SqlDataSource1" runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:Data %>"
SelectCommand="SELECT [idpropiedad],[propietario]
FROM [dbo].[Propiedades]
WHERE [idpropiedad] = 525293">
<SelectParameters>
<asp:ControlParameter name="idpropiedad"
controlid="idpropiedadBox" propertyname="Text" />
<asp:ControlParameter name="propietario"
controlid="propietarioBox" propertyname="Text" />
</SelectParameters>
</asp:SqlDataSource>
<asp:TextBox id="idpropiedadBox" runat="server"
DataSourceID="SqlDataSource1"></asp:TextBox>
<asp:TextBox id="propietarioBox" runat="server"
DataSourceID="SqlDataSource1"></asp:TextBox>
</div>
</form>
</body>
</html>
I have read so many articles about this but my application is on WebForms, not mvc, so I would like to do it this way.
Also, I have so many fields with responsive html, so I need to not use gridview.
It seems to me that I lost the dataview and that's what I need. I got used to working with databind on Visual Basic 6.0, but here it seems a little tricky and can't get it to work.
Maybe one of my problems is that I don't understand the WebForm Cycles times for Page Loading and others cycles times like pre render?
You need a databound server control to show / edit / update data. For a single row asp:FormView is OK.
You need to bind child controls to underlying fields. Eval for one-way binding, Bind for two-way binding.
The code may look like this.
<asp:FormView runat="server" DefaultMode="Edit" DataSourceID="SqlDataSource1">
<EditItemTemplate>
<asp:TextBox ID="idpropiedadBox" runat="server" Text='<%#Bind("idpropiedad") %>'></asp:TextBox>
<asp:TextBox ID="propietarioBox" runat="server" Text='<%#Bind("propietario") %>'></asp:TextBox>
<asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:Data%>"
SelectCommand="SELECT [idpropiedad],[propietario] FROM [dbo].[Propiedades] WHERE [idpropiedad] = 525293 "
UpdateCommand="update [dbo].[Propiedades] set [idpropiedad]=#idpropiedad,[propietario]=#propietario WHERE [idpropiedad] = 525293">
<SelectParameters>
<%--no need as select command doesn't have parameters --%>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="idpropiedad" Type="Int32" />
<asp:Parameter Name="propietario" Type="String" Size="1024" />
</UpdateParameters>
</asp:SqlDataSource>

Passing a Request.QueryString to a SelectParameters DefaultValue when the Request.Query String is a Integer

What I want to do seems simple but I am struggling with finding sample codes to work. My application is in ASP.NET VB. I am trying to pass a selected record from a table to a new page and then have the SQL Query select the record so that I can populate the field for editing. So far I am passing the record indexed ID to the second page. I can show that in a label field just for testing. But I am having issues that value to a SQL Datasource Select Command. Below is the code that works if I add a record number 4 to the Default Value. But I need to replace the Default value with a request.querystring.("VenueID"). Most of my attempts show an error that the SQL request could not be converted from a string to in Integer. Here is my code.
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Admn-VenueEdit.aspx.vb" Inherits="Admn_VenueEdit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<Form runat="server">
<%
Dim QueryID As String
QueryID = Request.QueryString("VenueID")
Dim URLQueryID
URLQueryID = Convert.ToInt64(QueryID)
'esponse.Write(QueryID)
'Dim VenueQueryString As String
'VenueQueryString = "SELECT * FROM [tblVenue] Where [ID] = " + Request.QueryString("VenueID")
%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" DataKeyNames="Id" Width="180">
    <Columns>
        <asp:TemplateField HeaderText="Destination" ItemStyle-Width="80">
            <ItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Destintion") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Company") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [tblVenue] Where [ID] = #VenueID">
<SelectParameters>
<asp:SessionParameter
Name="VenueID"
SessionField="VenueID"
DefaultValue="4"
/>
</SelectParameters>
</asp:SqlDataSource>
</Form>
</body>
The problem is here:
<asp:SessionParameter
Name="VenueID"
SessionField="VenueID"
DefaultValue="4"
/>
It always work with number 4. I doesn't matter what your query string is.
But if you need the query string to use in data source, There is an easy way to do this in sqldataSource visually configuration. When you going to it's settings, after set connection string, click next button, in next page you have a button Where click to reach this page:
After doing all, Click add button and close to back.
Your data source must be some thing like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FinalDentistConnectionString1 %>" SelectCommand="SELECT * FROM [ArticleComments] WHERE ([Id] = #Id)">
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

How can I get my columns renamed (alias) in my gridview?

I'm a bit stumped here trying to change the alias of the column names for C# asp.net page in Visual Studio 2012. This page let's users search/filter filter data by columns, TankNum, CL and date range. I want the gridview column name of TankNum to be Tank.
The full code is below:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView" DataSourceID="SqlDataSource1" CellPadding="5" AllowPaging="True" PageSize="5" runat="server" AutoGenerateColumns="true">
<HeaderStyle BackColor="#989898" ForeColor="white" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=Computer\sqlexpress;Initial Catalog=DB;Integrated Security=True"
SelectCommand="SELECT TankNum, GD, CL FROM tblHistory2"
FilterExpression="GD >= '#{0}#' AND GD <= '#{1}#' AND TankNum LIKE '{2}%' AND CL LIKE '{3}%'">
<FilterParameters>
<asp:ControlParameter Name="Date" ControlID="tbDateFrom" Type="DateTime" PropertyName="Text" />
<asp:ControlParameter Name=" Date" ControlID="tbDateTo" Type="DateTime" PropertyName="Text" />
<asp:ControlParameter ControlID="txtTankNum" Name="TankNum" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="txtCL" Name="CL" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
</FilterParameters>
</asp:SqlDataSource>
<br />
<th>Tank</th>
<td><asp:TextBox ID="txtTankNum" runat="server" Width="96px"> </asp:TextBox>Clock </td>
<td ><asp:TextBox ID="txtCL" runat="server" Width="104px"></asp:TextBox></td>
<p></p>
Enter the Date Range:
<p>
</p>
<td><asp:TextBox ID="tbDateFrom" runat="server" ReadOnly = "false">01/01/2000</asp:TextBox></td>
<td > </td>
<td><asp:TextBox ID="tbDateTo" runat="server" ReadOnly = "false">12/31/2017</asp:TextBox></td>
<input type="submit" id="Submit1" runat="server" />
</div>
</form>
</body>
</html>
I tried to do:
SelectCommand="SELECT TankNum AS Tank, GD, CL FROM tblHistory2"
but I get this error: Cannot find column [TankNum].
Again, this works fine I just can't figure how to change the alias names of the columns. Any suggestions?

Cannot access Ajax Rating from code behind

I have this html markup:
<div id="profil_rating_container">
<div id="profil_rating">
<div id="pr">
<h2 id="rating" class="profil_rating_title">Hodnocení <asp:Label ID="lblAddRating" runat="server"><span><i class="fa fa-plus-circle"></i></span></asp:Label></h2>
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:Rating ID="FinalRating" runat="server" MaxRating="5" ReadOnly="true" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar" FilledStarCssClass="mainFilledRatingStar" EmptyStarCssClass="mainEmptyRatingStar" />
<br />
<asp:SqlDataSource ID="RatinSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SportovniPlanetaCS %>"
SelectCommand="SELECT [RatingText], [RatingCount], [Name] FROM [rating] WHERE ([ProfileId] = #ProfileId) AND ([RatingText] IS NOT NULL)">
<SelectParameters>
<asp:QueryStringParameter Name="ProfileId" QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="RatingRepeater" DataSourceID="RatinSqlDataSource" runat="server" OnItemDataBound="RatingRepeater_ItemDataBound">
<ItemTemplate>
<h4><asp:Literal ID="RatingName" runat="server" Text='<%# Eval("Name") %>' /></h4>
<p class="p_rating"><asp:Literal ID="RatingText" runat="server" Text='<%# Eval("RatingText") %>'></asp:Literal></p>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblRating" runat="server" Text="Uživatel zatím nemá žádné hodnocení." Visible="false"></asp:Label>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</div>
And when I am accesing Ajax Control Toolkit Rating from code behind like this: FinalRating.CurrentRating = cmdRating.ExecuteScalar it underline FinalRating with error text: 'Final Rating' is not declared. It may be inaccessible due to its protection level.
I don't know why this is happening. I have the same code in other 5 pages and it is working finec, but in this one page it isn't. Also when I add new page and add Ajax Rating, I cannot access it from code behind. The rating control isn't in any data component like repeater or datalist. Can this be solved by FindControl? If yes, how exactly?
Does anyone know, what is causing this problem? Thanks for answer.

Trouble filtering a GridView using a DropDown Control

I'm trying to filter a gridview using a dropdown, but it appears that the dropdown value that is getting selected isn't making it to my SqlDataSource control. (I'm new to asp.net and this is a tutorial I'm working on.)
When I remove the SelectParameters section and plug one a valid LastName into the where clause of the gridview's select statement, I get a filtered gridview as I would expect, but it doesn't work when I'm selecting a name from the dropdown.
This may be a VS version thing. I'm using VS 2010 (w/.Net 3.5) where the book uses VS 2008.
The code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="SqlDataSourceParameters_3.aspx.cs" Inherits="SqlDataSourceWizard" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:SqlDataSource ID="CustomersDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
SelectCommand="SELECT [FirstName], [LastName], [EmailAddress], [ModifiedDate] FROM [Person].[Contact] WHERE ([LastName] = #LastName)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1"
Name="LastName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="CustomerGridView" runat="server" AutoGenerateColumns="False"
DataSourceID="CustomersDataSource" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress"
SortExpression="EmailAddress" />
<asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate"
SortExpression="ModifiedDate" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="StaffDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString2 %>"
SelectCommand="SELECT DISTINCT [LastName] FROM [Person].[Contact] ORDER BY 1"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="StaffDataSource" DataTextField="LastName"
DataValueField="LastName">
</asp:DropDownList>
</form>
</body>
</html>
Maybe I am missing something here but what is causing a postback? I do not see a button nor do I see the dropdown's autopostback property set to true. Is your code behind doing something (like setting the default value of the dropdown)?

Resources