Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > ทำ search database แล้วแสดง เป็น gridview ASP.NET C#


 

[.NET] ทำ search database แล้วแสดง เป็น gridview ASP.NET C#

 
Topic : 024861



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์



ขอเป็น code simple ให้ดูก็ดีครับ



Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-02-19 22:22:04 By : mamme View : 11644 Reply : 13
 

 

No. 1



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


ใช้ sql server 2005 express ครับ
Date : 2009-02-19 22:22:36 By : mamme
 

 

No. 2



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ASP.NET & Search Record ดู VB.NET ไปก่อนน่ะครับ C# คงจะมีภายในเร็ว ๆ นี้ครับ
Date : 2009-02-20 00:10:17 By : webmaster
 

 

No. 3



โพสกระทู้ ( 219 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

มาแล้วครับ C#
อันนี้ผมเคยทำไว้ เป็นการ search หา customer น่ะ

แต่ก่อนจะเอาไปใช้รบกวนศึกษา พวก event ของ gridview นิดนึงนะครับ

Code (C#)
001.using System;
002.using System.Collections;
003.using System.Configuration;
004.using System.Data;
005.using System.Data.SqlClient;
006.using System.Linq;
007.using System.Text;
008.using System.Web;
009.using System.Web.Security;
010.using System.Web.UI;
011.using System.Web.UI.HtmlControls;
012.using System.Web.UI.WebControls;
013.using System.Web.UI.WebControls.WebParts;
014.using System.Xml.Linq;
015. 
016.public partial class AdminCustSearch : System.Web.UI.Page
017.{
018.    DataSet ds = new DataSet();
019.     
020.    protected void Page_Load(object sender, EventArgs e)
021.    {
022.        if (!IsPostBack)
023.        {
024.            string strConn1 = "Data Source=MIXAR-PC;Initial Catalog=StockProductInfo;Integrated Security=True";
025. 
026.            SqlConnection Conn1 = new SqlConnection(strConn1);
027.            if (Conn1.State == ConnectionState.Open)
028.            {
029.                Conn1.Close();
030.            }
031.            Conn1.ConnectionString = strConn1;
032.            Conn1.Open();
033. 
034.            string sqlCustType;
035.            sqlCustType = "SELECT DISTINCT CustType_ID, CustType_Name ";
036.            sqlCustType += "FROM tabCustomerType ";
037.            sqlCustType += "WHERE (CustType_Status LIKE 'yes')";
038.            sqlCustType += "ORDER BY CustType_Name";
039. 
040.            SqlDataAdapter da1 = new SqlDataAdapter(sqlCustType, Conn1);
041.            DataSet ds1 = new DataSet();
042.            da1.Fill(ds1, "CustType");
043.            ddlCustomerType.DataSource = ds1.Tables["CustType"];
044.            ddlCustomerType.DataValueField = "CustType_Name";
045.            ddlCustomerType.DataTextField = "CustType_Name";
046.            ddlCustomerType.DataBind();
047.            ListItem firstItem = new ListItem("Select...", "Select...");
048.            ddlCustomerType.Items.Insert(0, firstItem);
049. 
050.            Conn1.Close();
051.        }
052.    }
053. 
054.    protected void btnSearch_Click(object sender, EventArgs e)
055.    {
056.        string strConn2 = "Data Source=MIXAR-PC;Initial Catalog=StockProductInfo;Integrated Security=True";
057. 
058.        SqlConnection Conn2 = new SqlConnection(strConn2);
059.        if (Conn2.State == ConnectionState.Open)
060.        {
061.            Conn2.Close();
062.        }
063.        Conn2.ConnectionString = strConn2;
064.        Conn2.Open();
065. 
066.        string sqlCustSearch;
067.        sqlCustSearch = "SELECT DISTINCT masCustomer.Cust_ID AS CustID, ";
068.        sqlCustSearch += "masCustomer.Cust_FName AS Firstname, ";
069.        sqlCustSearch += "masCustomer.Cust_LName AS Lastname, ";
070.        sqlCustSearch += "masCustomer.Cust_Tel AS Telephone, ";
071.        sqlCustSearch += "tabCustomerType.CustType_Name AS Type ";
072.        sqlCustSearch += "FROM masCustomer, tabCustomerType ";
073.        sqlCustSearch += "WHERE (masCustomer.CustType_ID = tabCustomerType.CustType_ID)";
074. 
075.        if (ddlCustomerType.SelectedItem.Value != "Select...")
076.        {
077.            sqlCustSearch += "AND (tabCustomerType.CustType_Name = '" + ddlCustomerType.SelectedItem.Text + "')";
078.        }
079.        if (txtCustID.Text != "")
080.        {
081.            sqlCustSearch += "AND (masCustomer.Cust_ID LIKE '" + txtCustID.Text + "%')";
082.        }
083.        if (txtFirstname.Text != "")
084.        {
085.            sqlCustSearch += "AND (masCustomer.Cust_FName LIKE '" + txtFirstname.Text + "%')";
086.        }
087.        if (txtLastname.Text != "")
088.        {
089.            sqlCustSearch += "AND (masCustomer.Cust_LName LIKE '" + txtLastname.Text + "%')";
090.        }
091.        if (txtTelephone.Text != "")
092.        {
093.            sqlCustSearch += "AND (masCustomer.Cust_Tel LIKE '" + txtTelephone.Text + "%')";
094.        }
095. 
096.        SqlDataAdapter da3 = new SqlDataAdapter(sqlCustSearch, Conn2);
097.        da3.Fill(ds, "Customers");
098. 
099.        dgvCustResult.DataSource = ds.Tables["Customers"];      //เอาไปโชว์ในดาต้ากริดวิว
100.        dgvCustResult.DataBind();
101.         
102.        if (dgvCustResult.Rows.Count <= 0)
103.        {
104.            Response.Write("<script type='text/javascript'>alert('Search not found!!');</script>");
105.            ddlCustomerType.SelectedValue = "Select...";
106.            txtCustID.Text = "";
107.            txtFirstname.Text = "";
108.            txtLastname.Text = "";
109.            txtTelephone.Text = "";
110.        }
111.        else
112.        {
113.            dgvCustResult.Visible = true;
114.        }
115. 
116.        Conn2.Close();
117.    }
118. 
119.    //method นี้ ผมมีปุ่ม select เป็น button field ไว้สำหรับดึงค่า ไปโชว์อีกเพจอะครับ
120.    protected void dgvCustResult_SelectedIndexChanged(object sender, EventArgs e)
121.    {
122.        string custID, custFName, custLName, custName;
123.        custID = dgvCustResult.SelectedRow.Cells[0].Text;
124.        custFName = dgvCustResult.SelectedRow.Cells[1].Text;
125.        custLName = dgvCustResult.SelectedRow.Cells[2].Text;
126.        custName = custFName + " " + custLName;
127. 
128.        Session["custFullName"] = custName;
129.        Session["custID"] = custID;
130. 
131.        Response.Redirect("AdminNewOrder.aspx");
132.    }
133.}


แอด เอ็ม มา ถามได้ครับ
ถ้าตอบได้จะช่วยตอบ
mixarstudio@live.com
Date : 2009-02-20 08:41:06 By : mixarstudio
 

 

No. 4



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (C#)
001.<%@ Import Namespace="System.Data"%>
002.<%@ Import Namespace="System.Data.SqlClient"%>
003.<%@ Page Language="C#" Debug="true" %>
004.<script runat="server">
005.        String strKeyWord;
006.        void Page_Load(object sender,EventArgs e)      
007.        {
008.            strKeyWord = this.txtKeyWord.Text;
009.        }
010. 
011.        void BindData()
012.        {
013.            SqlConnection objConn = new SqlConnection();
014.            SqlCommand objCmd = new SqlCommand();
015.            SqlDataAdapter dtAdapter = new SqlDataAdapter();
016.            DataSet ds = new DataSet();
017.            String strConnString,strSQL;
018. 
019.            strConnString = "Server=localhost;Uid=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
020.            strSQL = "SELECT * FROM customer WHERE (Name like '%"+ strKeyWord +"%' OR Email like '%"+ strKeyWord +"%') ";
021. 
022.            objConn.ConnectionString = strConnString;
023.            objCmd.Connection = objConn;
024.            objCmd.CommandText = strSQL ;
025.            objCmd.CommandType = CommandType.Text;
026. 
027. 
028.            dtAdapter.SelectCommand = objCmd;
029. 
030.            dtAdapter.Fill(ds);
031. 
032.            //*** BindData to GridView ***//
033.            myGridView.DataSource = ds;
034.            myGridView.DataBind();
035. 
036.            dtAdapter = null;      
037.            objConn.Close();
038.            objConn = null;
039. 
040.    }
041. 
042.    void myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
043.    {
044.        //*** CustomerID ***//
045.        Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
046.        if (lblCustomerID != null)
047.        {
048.            lblCustomerID.Text = (string)DataBinder.Eval(e.Row.DataItem, "CustomerID");
049.        }
050. 
051.        //*** Email ***//
052.        Label lblName = (Label)(e.Row.FindControl("lblName"));
053.        if (lblName != null)
054.        {
055.            lblName.Text = (string)DataBinder.Eval(e.Row.DataItem, "Name");
056.        }
057. 
058.        //*** Name ***//
059.        Label lblEmail = (Label)(e.Row.FindControl("lblEmail"));
060.        if (lblEmail != null)
061.        {
062.            lblEmail.Text = (string)DataBinder.Eval(e.Row.DataItem, "Email");
063.        }
064. 
065.        //*** CountryCode ***//
066.        Label lblCountryCode = (Label)(e.Row.FindControl("lblCountryCode"));
067.        if (lblCountryCode != null)
068.        {
069.            lblCountryCode.Text = (string)DataBinder.Eval(e.Row.DataItem, "CountryCode");
070.        }
071. 
072.        //*** Budget ***//
073.        Label lblBudget = (Label)(e.Row.FindControl("lblBudget"));
074.        if (lblBudget != null)
075.        {
076.            lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "Budget").ToString();
077.        }
078. 
079.        //*** Used ***//
080.        Label lblUsed = (Label)(e.Row.FindControl("lblUsed"));
081.        if (lblUsed != null)
082.        {
083.            lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "Used").ToString();
084.        }
085.    }
086. 
087.    void btnSearch_Click(Object sender, EventArgs e)
088.    {
089.        BindData();
090.    }
091. 
092.</script>
093.<html>
094.<head>
095.    <title>ThaiCreate.Com ASP.NET - SQL Server</title>
096.</head>
097.<body>
098.    <form id="form1" runat="server">
099.        <asp:Label id="lblKeyword" runat="server" text="Keyword"></asp:Label>
100.        <asp:TextBox id="txtKeyWord" runat="server"></asp:TextBox>
101.        <asp:Button id="btnSearch" onclick="btnSearch_Click" runat="server" Text="Search"></asp:Button>
102.        <br />
103.        <br />
104.        <asp:GridView id="myGridView" runat="server"
105.        AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound">
106.            <HeaderStyle backcolor="#cccccc"></HeaderStyle>
107.            <AlternatingRowStyle backcolor="#e8e8e8"></AlternatingRowStyle>
108.            <Columns>
109.                <asp:TemplateField HeaderText="CustomerID">
110.                    <ItemTemplate>
111.                        <asp:Label id="lblCustomerID" runat="server"></asp:Label>
112.                    </ItemTemplate>
113.                </asp:TemplateField>
114.                <asp:TemplateField HeaderText="Name">
115.                    <ItemTemplate>
116.                        <asp:Label id="lblName" runat="server"></asp:Label>
117.                    </ItemTemplate>
118.                </asp:TemplateField>
119.                <asp:TemplateField HeaderText="Email">
120.                    <ItemTemplate>
121.                        <asp:Label id="lblEmail" runat="server"></asp:Label>
122.                    </ItemTemplate>
123.                </asp:TemplateField>
124.                <asp:TemplateField HeaderText="CountryCode">
125.                    <ItemTemplate>
126.                        <asp:Label id="lblCountryCode" runat="server"></asp:Label>
127.                    </ItemTemplate>
128.                </asp:TemplateField>
129.                <asp:TemplateField HeaderText="Budget">
130.                    <ItemTemplate>
131.                        <asp:Label id="lblBudget" runat="server"></asp:Label>
132.                    </ItemTemplate>
133.                </asp:TemplateField>
134.                <asp:TemplateField HeaderText="Used">
135.                    <ItemTemplate>
136.                        <asp:Label id="lblUsed" runat="server"></asp:Label>
137.                    </ItemTemplate>
138.                </asp:TemplateField>
139.            </Columns>
140.        </asp:GridView>
141.    </form>
142.</body>
143.</html>


Ref : (C#) ASP.NET SQL Server Search Record
Date : 2009-09-24 21:29:46 By : webmaster
 

 

No. 5



โพสกระทู้ ( 4 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุนสำหรับข้อมูลดีๆ
Date : 2009-10-08 12:13:56 By : tandanai
 

 

No. 6



โพสกระทู้ ( 32 )
บทความ ( 0 )



สถานะออฟไลน์


w'
Date : 2009-12-24 10:50:34 By : byeday
 

 

No. 7



โพสกระทู้ ( 32 )
บทความ ( 0 )



สถานะออฟไลน์


รันไม่ผ่านเลย
Date : 2009-12-24 13:55:43 By : byeday
 

 

No. 8



โพสกระทู้ ( 32 )
บทความ ( 0 )



สถานะออฟไลน์


ค่ามันไม่ตรงกัน
Date : 2009-12-24 13:57:12 By : byeday
 

 

No. 9



โพสกระทู้ ( 179 )
บทความ ( 0 )



สถานะออฟไลน์


Error อะไร ครับ
Date : 2009-12-24 14:14:12 By : ksillapapan
 

 

No. 10



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณสำหรับข้อมูลต่างๆ
Date : 2010-03-22 15:00:09 By : winnamo
 

 

No. 11



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณคาบ
Date : 2011-10-05 19:00:15 By : k.singha
 

 

No. 12

Guest


อยากได้โค้ดค้นหา แบบ radio
Date : 2017-05-30 12:18:10 By : จินดา
 

 

No. 13

Guest


อยากได้โค้ดค้นหาแบบ radio อ่ะค่ะ
Date : 2017-05-30 12:19:36 By : จินดา
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ทำ search database แล้วแสดง เป็น gridview ASP.NET C#
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่