Thursday, March 29, 2012

Please give me Parameter Examples in ASP.net 2

Please some one help me. I'm trying to learn something very very simple and
I just have to see how it's done. If you're using VS2005 or VWD, please
create this very simple asp.net page for me and just past the whole source
code so I can see how it's done. People keep giving little bitsof code here and there saying try this or this and none of it isworking. Please let me see a completed source code for thissimple asp page.

This is all i want to do.
DB = SQL northwind
Table = Customer
Column = Contact Name
One Textbox = textbox1 with default value of "c"

I just want to see how you can pass a variable to the Select using Like
that contains the value in the textbox1.text. The result will show on a
simple GridView. I know I basically have to do it with parameters and I've
read a lot and tried a lot and can not get it to work. This is the Select
from VS2005. Can someone tell me if this is correct? My peramter name is
@dotnet.itags.org.test. The SQLDataSource is fine.

SelectCommand="SELECT [ContactName] FROM [Customers] WHERE ([ContactName]
LIKE '%' + @dotnet.itags.org.test + '%')"

Please please please create this simple aspx and paste the whole source code
so I can see how it's done.

Thanks in advance.

PhilVS2003 I'm afraid, namespace is SampleWebApp. HTML page;

<%@. Page language="c#" Codebehind="NWTest.aspx.cs" AutoEventWireup="false" Inherits="SampleWebApp.NWTest" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML><HEAD><title>NWTest</title><meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"><meta name="CODE_LANGUAGE" Content="C#"><meta name="vs_defaultClientScript" content="JavaScript"><meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"></HEAD><body><form id="Form1" method="post" runat="server"><asp:TextBox ID="txtText" Runat="server">c</asp:TextBox><p><asp:DataGrid ID="ResultsGrid" AutoGenerateColumns="True" Runat="server" /></p><p><asp:Button ID="cmdSubmit" Runat="server" Text="Search" /></p></form></body></HTML>

Code-behind;

using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace SampleWebApp{/// <summary>/// Summary description for NWTest./// </summary>public class NWTest : System.Web.UI.Page{protected System.Web.UI.WebControls.Button cmdSubmit;protected System.Web.UI.WebControls.DataGrid ResultsGrid;protected System.Web.UI.WebControls.TextBox txtText;private void Page_Load(object sender, System.EventArgs e){if (Page.IsPostBack){// Define our connection stringstring connection = "server=localhost;database=northwind;uid=sa;pwd=;";// Create a connection objectSqlConnection con = new SqlConnection(connection);// Create a command object for our SELECT, passing the connection objectSqlCommand com = new SqlCommand("SELECT [ContactName] FROM [Customers] WHERE [ContactName] LIKE '%' + @.test + '%'", con);// Creat a parameter to hold the value of @.testSqlParameter p = com.Parameters.Add ("@.test", SqlDbType.VarChar, 50);// Give it a valuep.Value = txtText.Text.Trim();// Open the connectioncon.Open();// Get a DataReader from the commandSqlDataReader r = com.ExecuteReader();// Give the DataReader to the DataGrid as a data sourceResultsGrid.DataSource = r;// Bind the DataGrid, causing it to renderResultsGrid.DataBind();// Close the connectioncon.Close();}}#region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){//// CODEGEN: This call is required by the ASP.NET Web Form Designer.//InitializeComponent();base.OnInit(e);}/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){ this.Load += new System.EventHandler(this.Page_Load);}#endregion}}

Thanks, but it didn't work. I'm using VS2005 and the code is giving me all sort of errors.

Can someone out there using VS2005 or VWD please give me this working example. PLLLEEEAAAASSSSEEE and thank you.
Can't you just create a new web page and cut and paste in only the relevant code? I doubt 2.0 is so far different that complete basics no longer work.
There have been some changes to the @.Page directive and <Head> sections in 2005. The "Web Form Designer generated code" doesn't appear in 2005 unless you specify that you don't want partial classes used. I'd do what Aidy suggested and just cut and paste the pertinent sections (the <form> to </form> and page_load event) into a page you've created.

0 comments:

Post a Comment