Monday, 6 April 2015

Fill Gridview with parameters


Class file Code .cs


public void FillGridprocedure(string SPName, string[] objParams, object[] objValues, GridView gv)
        {
            _objConnection = this.GetConnection();
            _objCommand = new SqlCommand(SPName, _objConnection);
            _objCommand.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < objParams.Length; i++)
            {
                _objParameter = new SqlParameter(objParams[i], objValues[i]);
                _objCommand.Parameters.Add(_objParameter);
            }

            _objConnection.Open();
            SqlDataAdapter objDataAdapter1 = new SqlDataAdapter(_objCommand);
            DataTable _objDataTable1 =new DataTable();
            objDataAdapter1.Fill(_objDataTable1);
            _objConnection.Close();
            if (_objDataTable1 != null && _objDataTable1.Rows.Count > 0 && _objDataTable1.Rows[0][0].ToString() != null)
            {
                gv.DataSource = _objDataTable1;
            gv.DataBind();
            }else
            {
                gv.EmptyDataText="No Record Found ......";
            }
        }


In ButtonClick Event

 protected void btnsearch_Click(object sender, EventArgs e)
    {
        string[] Parameter = { "@V_name", "@Phone_No", "@City", "@Service_Area" };
        string[] Values = { txtvendor.Text, txtmobile.Text, txtcity.Text, txtservice.Text };
        objDatalayer.FillGridprocedure("Sp_SearchVendor", Parameter, Values, GridView1);

    }



No comments:

Post a Comment