简体   繁体   中英

Gridview Search box in ASP.NET

I have made search box using textbox and button control to search the data in my GridView, for datasource I'm using ObjectDataSource. In ObjectDataSource Class I'm Using parameterized procedure to select data from database table, but the problem was occured here, ObjectDataSource expect a value for parameter class . I have solved this with hardcoded the class if it null give the parameter value equals to white space, it works good.

If there is another way solve this without hardcoded the class, any answers would be helpful, thanks

Here is my ObjectDataSource Select Class

public static List<T_Penerbit> GetSearchPenerbit(string Cari)
        {
            if (string.IsNullOrWhiteSpace(Cari))
            {
                Cari = " ";
            }

            List<T_Penerbit> listSearchPenerbit = new List<T_Penerbit>();

            string cs = ConfigurationManager.ConnectionStrings["cs_perpustakaan"].ConnectionString;

            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spGetPenerbitBySearch", con);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter paramSearch = new SqlParameter("@parameter", Cari);
                cmd.Parameters.Add(paramSearch);

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    T_Penerbit penerbit = new T_Penerbit();
                    penerbit.ID = Convert.ToInt32(rdr["ID"]);
                    penerbit.Penerbit = rdr["Nama_Penerbit"].ToString();
                    penerbit.Kota = rdr["Kota"].ToString();
                    penerbit.Handphone = rdr["Handphone"].ToString();
                    penerbit.Email = rdr["Email"].ToString();

                    listSearchPenerbit.Add(penerbit);
                }
            }
            return listSearchPenerbit;
        }

And here is my button Search Click event

protected void ButtonKelolaDataPenerbitCariPenerbit_Click(object sender, EventArgs e)
        {
            ObjectDataSourceCariDataPenerbit.SelectParameters.Clear();
            ObjectDataSourceCariDataPenerbit.SelectParameters.Add("Cari", TextBoxKelolaDataPenerbitCariPenerbit.Text);

            ObjectDataSourceCariDataPenerbit.DataBind();
        }

Presentation changes :

 <div style="margin-top:50px">
        SEARCHING
        <br /><br />
        Enter Id : - 
        <asp:TextBox ID="txtSearchId" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
    </div>

Back-end updates :

protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["StudDBConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = "select * from tbl_stud where id="+txtSearchId.Text;
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
        dataTable = new DataTable();
        sqlDataAdapter.Fill(dataTable);
        grvStudentWithoutDatasource.DataSource = dataTable;
        grvStudentWithoutDatasource.DataBind();
    }

Note: The given code is according to my database, Please change database column accordingly.

try this

aspx file:

<asp:ObjectDataSource runat="server" ID=" ObjectDataSourceCariDataPenerbit" TypeName="Penerbit" SelectMethod="GetSearchPenerbit">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBoxKelolaDataPenerbitCariPenerbit" Name="Cari" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

button Search Click event

protected void ButtonKelolaDataPenerbitCariPenerbit_Click(object sender, EventArgs e)
        {

            ObjectDataSourceCariDataPenerbit.DataBind();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM