QueryString Parameter - asp.net

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>

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>

The server tag is not well formed. QueryString and DataBinding

I've done a ton of research but with no luck finding a way to resolve this.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" EnableModelValidation="True">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="(SELECT [TID], [CaseID]
,[dtTimeStamp]
,[Site]
,[Dept]
,[LocationDetail]
,[ContactPerson]
,[Phone]
,[contactEmail]
,[ProblemDesc]
FROM [hispcsupport_kb].[dbo].[tblCase]
WHERE CaseID=" Request.QueryString("CaseID"))">
</asp:SqlDataSource>
</div>
</form>
From the error, this is the line causing the error.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="(SELECT [TID], [CaseID]
Any help?
Change the SelectCommand to use a parameter: Using Parameters with the SqlDataSource Control, which will have the added benefit of removing the chance of an SQL injection attack. The "Community Additions" section at the end of that shows how to add the parameter with a value.
So...
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="(SELECT [TID], [CaseID]
,[dtTimeStamp]
,[Site]
,[Dept]
,[LocationDetail]
,[ContactPerson]
,[Phone]
,[contactEmail]
,[ProblemDesc]
FROM [hispcsupport_kb].[dbo].[tblCase]
WHERE CaseID = #CaseID">
</asp:SqlDataSource>
and in an appropriate place in the code-behind:
SqlDataSource1.SelectParameters.Add("#CaseID", DbType.NVarChar, Request.QueryString("CaseID"))
(please change the DbType if required).
The quotes around Request.QueryString("CaseID") are definitely one problem. You're already using double quotes for the SelectCommand element. Change the quotes around the SelectCommand to be single quotes:
SelectCommand='Select * from tblCase where CaseID=<%=Request.QueryString["CaseID"] %>'
Feel free to change the * back to a list of fields after you see that it works.

Creating a Separate Detail View Page in ASP.net

OK, so I've created a form that has 2 connecting drop down list. I have a button that redirects to another page. This one is currently blank, but I've tried adding a detail view to it, however, when I try and connect it to the data source it just shows the first of the column names and abc.
I need to know how to connect it to my data source which would create 4 rows and the corresponding information for those rows. In this case it would be something akin to Quantity, Category, Product Name, and the Description for the item selected on the first page.
Any help is seriously appreciated.
UPDATE
I still can't get this working.
Here is a screenshot of page one.
My instructions for this page are simply...
On the first screen, a user chooses a category and then chooses a product from the selected category. The Category and Product controls are populated using the following procedures: So I set up both of these with the respective procedures, and they work fine.
The order detail button simply redirects to the order detail page, but I'm not sure if I have to do anything to save the selection.
However, here is the code I've put together for those 3 buttons.
<div style="height: 182px">
Category: <asp:DropDownList ID="ddlCategory"
runat="server" DataSourceID="SqlDataSource1" DataTextField="CategoryName"
DataValueField="CategoryId" AutoPostBack="True"/>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:DeveloperInterviewConnectionString %>"
SelectCommand="CategoryListing" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<br/>
Product: <asp:DropDownList ID="ddlProduct"
runat="server" DataSourceID="SqlDataSource2" DataTextField="ProductName"
DataValueField="ProductId"/>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:DeveloperInterviewConnectionString %>"
SelectCommand="CategoryProducts" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory" Name="CategoryId" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<input type="button" value="Order Details"
onClick="location.href = 'OrderDetails.aspx';">
So do I have to do anything to setup the data to go from one form to the next, and I still can't get the view operational.
Ok, So I tried linking up your stored proc to a details view and didn't get very far with it. I think the problem is the output params.
Try modifying the stored proc to the following:
CREATE PROCEDURE [dbo].[ProductDetails]
#ProductId INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
ProductName,
ProductDescription,
QuantityInStock
from Product
where productid = #ProductId
END
GO
Point the datasource at that Sproc, link up the ProductID param and you should be set.
UPDATE: adding my asp.net code
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataSourceID="SqlDataSource1">
<Fields>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="ProductDescription" HeaderText="ProductDescription" SortExpression="ProductDescription" />
<asp:BoundField DataField="QuantityInStock" HeaderText="QuantityInStock" SortExpression="QuantityInStock" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DemoDBConnectionString %>" SelectCommand="ProductDetails" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="ProductId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

inserting data in sqlce from asp.net form

I am trying to insert names (string) in to a test database (sqlce). Basically, user will type in name in the textbox (input_box) and click on "Insert Button". Upon click of the button, a call is made to "submit" function. I want the text from the textbox to be used as the parameter for insertcommand. I don't get any error while running it. But when I check the data in the table, only "null" is inserted. I am trying to learn asp.net. Any help would be appreciated. Thank you.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title>Test run</title></head>
<script language="C#" runat = "server">
void Submit(object o, EventArgs e)
{
SqlDataSource1.Insert();
}
</script>
<body style="height: 395px">
<form id="form1" runat="server">
<asp:TextBox ID="Input_box" runat="server"></asp:TextBox>
<asp:Button ID="Insert_button" runat="server" Text=" Insert Button" OnClick = "Submit"/>
<asp:SqlDataSource
ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO Incident_Table(Assignee) VALUES (#assignee)"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT Assignee FROM Incident_Table">
<InsertParameters>
<asp:FormParameter Name="assignee" FormField ="name"/>
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
you should replace this:
<asp:FormParameter Name="assignee" FormField ="name"/>
with:
<asp:FormParameter Name="assignee" FormField="Input_box" />

Resources