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" />
Related
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>
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>
<%# 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
I am trying to pass a variable to a SelectCommand in SqlDataSource. I have this MyIdVal that need to be passed.
Here is the code :
<form id="form1" runat="server">
<div>
<%=MyIdVal%>
</div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="myIdDataSource">
</asp:GridView>
<asp:SqlDataSource runat="server" ID="myIdDataSource"
ConnectionString="<%$ ConnectionStrings:myCipConnection %>"
ProviderName="<%$ ConnectionStrings:myCipConnection.ProviderName %>"
SelectCommand="SELECT * FROM books WHERE id = #MyIdVal" >
</asp:SqlDataSource>
</form>
The code works fine if I hardcode the id, so how this works?
Here's two ways to do it.
Bind the Parameter value to a control
<selectparameters>
<asp:controlparameter
name="MyIdVal"
controlid="DropDownListBooks"
propertyname="SelectedValue"/>
</selectparameters>
2 . Use the SqlDataSource.Selecting Event
protected void myIdDataSource_Selecting
(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["MyIdVale"] = MyIdVal;
}
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>