Wednesday, May 23, 2018

Sample Select Web page c# gridview

Sample Select Web page c# gridview

Default.aspx:

asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"   DataKeyNames="BusinessEntityID"
        GridLines="None" AutoGenerateColumns="False"  AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" DataSourceID="SqlDataSource1">
        



connection string

            ConnectionString="<%$ ConnectionStrings:AdventureWorks2008R2ConnectionString %>"
            UpdateCommand= "update Person_test set BusinessEntityID=@BusinessEntityID,FirstName=@FirstName,MiddleName=@MiddleName,LastName=@LastName WHERE BusinessEntityID=@BusinessEntityID"
            DeleteCommand="Delete from Person_test where BusinessEntityID=@BusinessEntityID"

            >



Default.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT * FROM [Person_test]";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     //  SqlDataSource1.SelectCommand = "select * from Person_test where BusinessEntityID ='" + TextBox1.Text + "' or FirstName= '" + TextBox2.Text + "' or MiddleName= '" + TextBox3.Text + "' or LastName='" + TextBox4.Text + "'";
       SqlDataSource1.SelectCommand = "Select * from Person_test where BusinessEntityID like '%" + TextBox1.Text + "%'or FirstName like '%" + TextBox2.Text + "%'or MiddleName like '%" + TextBox3.Text + "%'or LastName like '%" + TextBox4.Text + "%'";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand= "Insert into person_testshweta([BusinessEntityID],[FirstName],[MiddleName],[LastName]) select '"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"'";
    }
}


Webconfig.aspx







    
                    providerName="System.Data.SqlClient" />
   
    
        
        
        
    




Sql command to create table


select BusinessEntityID,FirstName,LastName into Person_test  from   Person.Person

No comments: