What is wrong with this ASP.net DataBinding program? - asp.net

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>

Related

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>

ASP access database dynamic output

<%# 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

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)?

FormView.FindControl works for one form but not the other on the same page

I have two Formviews on the same page. I can use FindControl on the first without issue, but it always fails on the second one.
I am using the ItemTemplate for both, both default to ReadOnly, both are bound to SQLDataSources (different ones) but I cannot for the life of me work out why FindControl works for one and not the other.
I have removed a lot of the plain text from the code below.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Apps] WHERE ([AppID] = #AppID)" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="AppID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1>
<asp:Label ID="AppNameLabel" runat="server" Text='<%# Bind("AppName") %>' />
<asp:Label ID="VersionLabel" runat="server" Text='<%# Bind("Version") %>' /> for
<asp:Label ID="OSLabel" runat="server" Text='<%# Bind("OS") %>' />
</h1>
<p>Text here</p>
<p><asp:TextBox ID="LicenseTextBox" runat="server"
Text='<%# Eval("License")%>'
TextMode="MultiLine" Width="800px" Rows="25" ReadOnly="True"></asp:TextBox></p>
<asp:HiddenField ID="AppLocation" runat="server" ViewStateMode="Inherit" Value='<%# Bind("AppLocation") %>'/>
</ItemTemplate>
</asp:FormView>
<p><asp:Literal ID="SizeLiteral" runat="server"></asp:Literal></p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Installations] WHERE (([AppID] = #AppID) AND ([Username] = #Username))" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
<asp:FormParameter FormField="Username" Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="DLFormView" runat="server" DataSourceID="SqlDataSource4"
DataKeyNames="AppID,Username">
<ItemTemplate>
<p> <asp:Label ID="DLAppNameLabel" runat="server" />
<br />
<br />
<asp:Label ID="NumberOfInstallations" runat="server" Text='<%# Bind("Installations") %>'></asp:Label>
<br />
<asp:HiddenField ID="TotalInstallations" runat="server" />
Number of New Installations:
<asp:DropDownList ID="NumberOfNewInstallations" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="TotalNumberOfInstallations" runat="server" Text="Label"></asp:Label>
</p>
</ItemTemplate>
</asp:FormView>
And the FindControl coding is as follows...
TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");
HiddenField AppLocCode = (HiddenField)FormView1.FindControl("AppLocation");
Label AppNameCode = (Label)FormView1.FindControl("AppNameLabel");
These always work...
Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");
This always returns null. I've read a million bosts about the controls not being rendered until databinding has completed, but surely if the two FOrmViews are set up the same way, the result should be the same.
Any help would be much apreciated :)
Matt :)
I'd like to plead stupidity and but also provide some background to how I've used what I've learnt from my stupidity.
My SqlDataSource for DLFormView was returning records, of course I then used slightly different values by accident and so no records were returned. I only figured this out by making the ItemTemplate section of the form view only contain unbound (basically plain text) data. As nothing displayed, this pointed towards the page using a different tempalte. When I put a test line in the EmptyDataTemplate, it displayed, confirming there was nothing returned by the SqlDataSource. Perhaps a School boy error not putting something in this template during development.
As it happens I need to use the EmptyDataTemplate anyway, I just hadn't got to this point. As only one template is rendered at a time, I have been able to use the same ID names in both templates. This means there is no need to test for which template is being used. (although empty template is used when DataItemsCount = 0, otherwise you can test for CurrentMode for the remaining templates.)
So a big fail on my part, but I hope people can learn from my mistake.
Matt :)

QueryString Parameter

My code:
<html>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>"
SelectCommand="GetProductsByCategoryID"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID"
QueryStringField="CategoryID" />
</SelectParameters>
</html>
I'm using on my ASP.NET page a stored procedure.
But this page is giving a blank page it should give me when I click on the other page the products corresponding to the categoryId clicked all is working fine except this page is giving blank result.
If I put for example defaultValue="2" it gives me result so what I might be missing?
The link is built with such code:
<ItemTemplate> <%# Eval("CategoryName") %> </ItemTemplate>

Resources