Create a web application bind data in a multiline textbox by querying in another textbox

 Default.asspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 

Inherits="_6A.Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

</head> 

<body> 

    <form id="form1" runat="server"> 

        <div> 

        </div> 

        <p> 

            <asp:TextBox ID="TextBox1" runat="server" Height="69px" 

TextMode="MultiLine"></asp:TextBox> 

        </p> 

        <p> 

            &nbsp;</p> 

        <p> 

            <asp:ListBox ID="ListBox1" runat="server" Height="96px" SelectionMode="Multiple" 

Width="240px" DataSourceID="SqlDataSource1" DataTextField="Id" 

DataValueField="Id"></asp:ListBox> 

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ 

ConnectionStrings:connStr %>" SelectCommand="SELECT * FROM 

[Student]"></asp:SqlDataSource> 

        </p> 

        <p> 

            &nbsp;</p> 

        <p> 

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 

        </p> 

    </form> 

</body> 

</html> 

Default.aspx.cs 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using System.Data.SqlClient; 

using System.Data; 

using System.Configuration; 

namespace _6A 

    public partial class Default : System.Web.UI.Page 

    { 

        SqlConnection con = new 

        SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);         

        protected void Page_Load(object sender, EventArgs e) 

        { 

 

        } 

        protected void Button1_Click(object sender, EventArgs e) 

        { 

            con.Open(); 

            SqlCommand cmd = new SqlCommand(TextBox1.Text, con); 

            SqlDataReader reader = cmd.ExecuteReader(); 

            ListBox1.Items.Clear();  

            while (reader.Read()) 

            {                            

                for (int i = 0; i < reader.FieldCount - 1; i++)        

                {            

                    ListBox1.Items.Add(reader[i].ToString());        

                }       

            }     

            reader.Close();               

            con.Close(); 

        } 

    } 

Web.config 

<?xml version="1.0" encoding="utf-8"?> 

<!-- 

  For more information on how to configure your ASP.NET application, please visit   https://go.microsoft.com/fwlink/?LinkId=169433 

  --> 

<configuration> 

  <system.web> 

    <compilation debug="true" targetFramework="4.7.2"/> 

    <httpRuntime targetFramework="4.7.2"/> 

  </system.web> 

  <system.codedom> 

    <compilers> 

      <compiler language="c#;cs;csharp" extension=".cs"         type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 

        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/> 

      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" 

        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, 

PublicKeyToken=31bf3856ad364e35" 

        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 

/define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/> 

    </compilers> 

  </system.codedom> 

  <connectionStrings> 

    <add name="connStr" connectionString="Data 

Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\RupamSolution\source\repos

\6A\6A\App_Data\admin.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/> 

  </connectionStrings> 

</configuration> 

Create Database:- 


Insert Data into Table (Student): 


Output:- 



Post a Comment

0 Comments