简体   繁体   English

如何从数据集中删除数据表?

[英]How to remove datatable from dataset?

.aspx code: .aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchCustomer.aspx.cs" Inherits="WebApplication1.eyeofheaven.SearchCustomer" %>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" type="text/css" href="StyleSheets/SearchCustomerStyle.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<title>Search Customer</title>
</head>
<body>
<form id="form1" runat="server">
 <div class="row">
    <div class="twelve columns">
            <!-- Header-->
            <div class="container">
                <nav role="navigation" class="navbar navbar-inverse navbar-fixed-top">
                    <!-- Brand and toggle get grouped for better mobile display -->
                    <div class="navbar-header">
                        <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                    </div>
                    <!-- Collection of nav links, forms, and other content for toggling -->
                    <div id="navbarCollapse" class="collapse navbar-collapse">
                        <ul class="nav navbar-nav">
                            <li><a href="EyeOfHeaven.aspx">Home</a></li>
                            <li class="dropdown">
                                <a data-toggle="dropdown" class="dropdown-toggle active" href="#">Search<b class="caret"></b></a>
                                <ul role="menu" class="dropdown-menu">
                                    <li><a href="SearchCustomer.aspx">Search Form(Customer)</a></li>
                                    <li><a href="SearchVehicle.aspx">Search Form(Vehicle)</a></li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                </nav>
            </div>
    </div>
</div>

<!-- Search form customer-->
<div id="searchcustomer" class="page-header">
    <h3><span class="glyphicon glyphicon-th-large"></span>Search Customer</h3>
</div>

<div class="row">
    <div class="col-md-4">
        <input type="text" runat="server" id="search" size="20" class="form-control" placeholder="Customer ID">
    </div>
    <div class="col-md-4">
        <select class="form-control" runat="server" id="Country">
            <option value="select" selected disabled>Search by Country</option>
            <option value="A:C ESTUDIO">A:C ESTUDIO</option>
            <option value="Aaron McEwen-194712">Aaron McEwen-194712</option>
            <option value="Accra">Accra</option>
            <option value="Adoany">Adoany</option>
            <option value="Aduanas">Aduanas</option>
            <option value="Alex Sanchez-259029">Alex Sanchez-259029</option>
            <option value="ALG Consulting-288078">ALG Consulting-288078</option>
            <option value="Algeria">Algeria</option>
            <option value="Algimantas Ramaskevicius">Algimantas Ramaskevicius</option>
        </select>
    </div>  

    <div class="col-md-4">
        <select class="form-control" runat="server" id="Currency">
            <option value="selected" selected disabled>Search by Currency</option>
            <option value="AUD">AUD (Australian Dollar)</option>
            <option value="EUR">EUR (Euro)</option>
            <option value="GBP">GBP (United Kingdom Pounds)</option>
            <option value="JPY">JPY (Japan Yen)</option>
            <option value="NZD">NZD (New Zealand Dollar)</option>
        </select>
    </div>
</div>

<div class="row">
    <div class="col-md-4">
<button type="button" runat="server" onserverclick="Button1_Click" id="searchinfo" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search Info</button>
<button type="button" runat="server" onserverclick="Button2_Click" id="Button2" class="btn btn-danger"><span class="glyphicon glyphicon-repeat"></span>Reset</button>
    </div>
</div>
<!-- Information Table-->
<div id="gridview">
    <asp:GridView runat="Server" id="data" CssClass="table table-striped table-bordered table-responsive">
    </asp:GridView>
</div>

</form>
</body>
</html>

.aspx code behind: .aspx代码背后:

using MSSQLConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.eyeofheaven
{
    public partial class SearchCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            MSConnector connector = new MSConnector();
            connector.ConnectionString = "SERVER=xbetasql,52292;UID=username;Password=secret;DATABASE=ATDBSQL;";

            string customer = (this.search.Value);
            string country = (this.Country.Value);
            string idcurrency = (this.Currency.Value);
            string query = null;

            if (country != "select")
            {
                if (idcurrency != "selected")
                {
                    query = "select * from customer where country = '" + country + "' and idcurrency = '" + idcurrency + "'";
                }
                else
                {
                    query = "select * from customer where country = '" + country + "'";
                }
            }
            DataSet selectedData = connector.ExecuteQuery(query);
            DataTable dt = selectedData.Tables[0];
            if (dt.Rows.Count > 0)
            {
                data.DataSource = dt;
                data.DataBind();
            }
            else
            {
                Response.Write("<script>alert('No Data Found.')</script>");
            }
        }
        protected void Button2_Click(object sender, EventArgs e, MSConnector connector, string query)
        {
            DataSet selectedData = connector.ExecuteQuery(query);
            DataTable dt = selectedData.Tables.Remove(dt);
            this.search.Value = "";
            this.Country.Value = "select";
            this.Currency.Value = "selected";

        }
    }
}

I have a problem in my code, I want to remove the datatable from dataset when I click on Button2_Click as a reset button for all.我的代码有问题,当我单击datatable作为所有重置按钮时,我想从dataset删除数据Button2_Click But I have an error in my code in here:但是我这里的代码有一个错误:

DataTable dt = selectedData.Tables.Remove(dt);

It says:它说:

Cannot implicitly convert type 'void' to 'System.Data.DataTable'无法将类型“void”隐式转换为“System.Data.DataTable”

Does passing my dataset and datatable is the problem on the function Button2_Click() ?传递我的dataset和数据表是 function datatable Button2_Click()上的问题吗?

You only need this你只需要这个

selectedData.Tables.Remove(dt);

But it would be better if you first check that DataSet contains the table which you are trying to remove.但是如果您首先检查DataSet包含您要删除的表会更好。 Also check if the DataTable can be removed from DataSet as pointed by Gianni B .还要检查是否可以从Gianni B指出的DataSet删除DataTable

if(selectedData.Tables.Contains(dt.name) && selectedData.Tables.CanRemove(dt))
   selectedData.Tables.Remove(dt);

Remove method does not return anything. Remove方法不返回任何东西。 It's return type is void You are trying to assign a datatable returned by Remove method while actually it doesn't return anything它的return typevoid您正在尝试分配由Remove方法datatable returned ,而实际上它不返回任何内容

The line which you have written你写的那一行

DataTable dt = selectedData.Tables.Remove(dt);

is wrong, as remove method does not return anything.是错误的,因为 remove 方法不返回任何内容。

So you need to change your code as所以你需要改变你的代码

selectedData.Tables.Remove(dt);
if(selectedData != null && selectedData.Tables.Count > 0)
{
   DataTable dt = selectedData.Tables[0];
}

Try this:尝试这个:

DataTable dtDelete = new DataTable();
dtDelete = ds_RecipeData.Tables[0]
if (ds_RecipeData.Tables.Contains(dtDelete.TableName))
    if (ds_RecipeData.Tables.CanRemove(dtDelete))
      ds_RecipeData.Tables.Remove(dtDelete);

It is not the same Datatable despite entering the TableName , so ds.Tables.Remove does not work correctly.尽管输入了TableNameds.Tables.Remove Datatable正常工作。 This works for me这对我有用

DataTable dt = new DataTable();

int i = ds.Tables.IndexOf(dt.TableName);

dt = ds.Tables[i];

ds.Tables.Remove(dt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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