请解决错误 [英] Please solve the error

查看:59
本文介绍了请解决错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译代码时遇到错误。



DataSource和DataSourceID都在'GridView1'上定义。删除一个定义。



我尝试过:



 使用系统; 
使用 System.Configuration;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Drawing;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;


public partial class viewfile:System.Web.UI.Page
{
private SqlConnection conn = new SqlConnection( 数据源=。\\sqlexpress;初始目录= college_education;用户ID = sa;密码=系统;);

受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
gvbind() ;
}
}

受保护 void gvbind( )
{
conn.Open();
SqlCommand cmd = new SqlCommand( 选择*来自file_rec,conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables [ 0 ]。Rows.Count > 0
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables [ 0 ]。Rows.Add(ds.Tables [ 0 ]。NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows [ 0 ]。Cells.Count;
GridView1.Rows [ 0 ]。Cells.Clear();
GridView1.Rows [ 0 ]。Cells.Add( new TableCell());
GridView1.Rows [ 0 ]。单元格[ 0 ]。ColumnSpan = columncount;
GridView1.Rows [ 0 ]。单元格[ 0 ]。Text = 找不到记录;
}

}

受保护 void GridView1_RowDeleting( object sender,GridViewDeleteEventArgs e)
{
GridViewRow row =(GridViewRow)GridView1.Rows [e.RowIndex];
标签lbldeleteid =(标签)row.FindControl( lblID);
conn.Open();
SqlCommand cmd = new SqlCommand( delete FROM file_rec其中serial_no =' + Convert.ToInt32(GridView1.DataKeys [e.RowIndex] .Value.ToString())+ ',conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();

}

受保护 void GridView1_RowEditing(< span class =code-keyword> object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
受保护 void GridView1_RowUpdating( object sender,GridViewUpdateEventArgs e)
{
int serial_no = Convert.ToInt32(GridView1.DataKeys [e.RowIndex]。 Value.ToString());
GridViewRow row =(GridViewRow)GridView1.Rows [e.RowIndex];
标签lblID =(标签)row.FindControl( lblID);
// TextBox txtname =(TextBox)gr.cell [] .control [];
TextBox textinternal_receiving =(TextBox)row.Cells [ 0 ]。控件[ 0 ];
TextBox textdate =(TextBox)row.Cells [ 1 ]。控件[ 0 ];
TextBox textdepartment =(TextBox)row.Cells [ 2 ]。控件[ 0 ];
TextBox textdepartment_type =(TextBox)row.Cells [ 3 ]。控件[ 0 ];
TextBox textorganization =(TextBox)row.Cells [ 4 ]。控制[ 0 ];
TextBox textorganization_type =(TextBox)row.Cells [ 5 ]。控制[ 0 ];
TextBox textfile_no =(TextBox)row.Cells [ 6 ]。控件[ 0 ];
TextBox textsubject =(TextBox)row.Cells [ 7 ]。控件[ 0 ];
TextBox textfile_name =(TextBox)row.Cells [ 8 ]。控件[ 0 ];
TextBox textstatus =(TextBox)row.Cells [ 9 ]。控件[ 0 ];
// TextBox textadd =(TextBox)row.FindControl(txtadd);
// TextBox textc =(TextBox)row.FindControl(txtc);
GridView1.EditIndex = -1;
conn.Open();
// SqlCommand cmd = new SqlCommand(SELECT * FROM detail,conn);
SqlCommand cmd = new SqlCommand( update file_rec set internal_receiving =' + textinternal_receiving + ',date =' + textdate + ',department =' + textdepartment + ',department_type =' + textdepartment_type + ',organization =' + textorganization + ',organization_type =' + textorganization_type + ',file_no =' + textfile_no + ',subject =' + textsubject + ',file_name =' + textfile_name + ',status =' + textstatus + 'where =' + serial_no + ',conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
// GridView1.DataBind();
}
< span class =code-keyword> protected void GridView1_PageIndexChanging( object sender,GridViewPageEventArgs e )
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();
}
protected void GridView1_RowCancelingEdit( object sender,GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();
}

}

解决方案

让我们先来看看 DataSource 和 DataSourceID

引用:

DataSource 是指实际数据源对象,它可以是.NET提供的数据源控件(如ObjectDataSource,SqlDataSource)或实际数据对象,如DataTable,对象集合等。 />


DataSourceID 是.NET提供的数据源控件的字符串标识符,并且此属性存在,以便可以关联数据绑定控件和相应的数据源在标记的设计时间。在内部,控件将使用提供的id查找实际的数据源控件。





1.确保,你没有提到 DataSourceID 在后面的代码中。类似的事情 -

 Gridview1.DataSourceID =   ds ; 



2.尝试 -

 GridView1.DataSource = ds.Table [ 0 ]; 



3.尝试 -

 Gridview1.DataSourceID =  null ; 
GridView1.DataSource = ds.Table [ 0 ];
GridView1.DataBind();





希望以上解决方案/解决方法之一适合您:)


< asp:GridView ID =GridView1runat =serverDataSourceID =whatever>中删除DataSourceID属性
< / asp:GridView>


i am getting below error while compiling code.

Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.

What I have tried:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class viewfile : System.Web.UI.Page
{
    private SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=college_education;User ID=sa;Password=system;");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvbind();
        }
    }

    protected void gvbind()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from file_rec", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lbldeleteid = (Label)row.FindControl("lblID");
        conn.Open();
        SqlCommand cmd = new SqlCommand("delete FROM file_rec where serial_no='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        gvbind();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        gvbind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int serial_no = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");
        //TextBox txtname=(TextBox)gr.cell[].control[];
        TextBox textinternal_receiving = (TextBox)row.Cells[0].Controls[0];
        TextBox textdate = (TextBox)row.Cells[1].Controls[0];
        TextBox textdepartment = (TextBox)row.Cells[2].Controls[0];
        TextBox textdepartment_type = (TextBox)row.Cells[3].Controls[0];
        TextBox textorganization = (TextBox)row.Cells[4].Controls[0];
        TextBox textorganization_type = (TextBox)row.Cells[5].Controls[0];
        TextBox textfile_no = (TextBox)row.Cells[6].Controls[0];
        TextBox textsubject = (TextBox)row.Cells[7].Controls[0];
        TextBox textfile_name = (TextBox)row.Cells[8].Controls[0];
        TextBox textstatus = (TextBox)row.Cells[9].Controls[0];
        //TextBox textadd = (TextBox)row.FindControl("txtadd");
        //TextBox textc = (TextBox)row.FindControl("txtc");
        GridView1.EditIndex = -1;
        conn.Open();
        //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
        SqlCommand cmd = new SqlCommand("update file_rec set internal_receiving='" + textinternal_receiving + "',date='" + textdate + "',department='" + textdepartment + "',department_type='" + textdepartment_type + "',organization='" + textorganization + "',organization_type='" + textorganization_type + "',file_no='" + textfile_no + "',subject='" + textsubject + "',file_name='" + textfile_name + "',status='" + textstatus + "'where ='" + serial_no + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        gvbind();
        //GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        gvbind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        gvbind();
    }

}

解决方案

Let's first look at the difference between DataSource and DataSourceID.

Quote:

DataSource refers to actual data source object which can be .NET provided data source controls (such as ObjectDataSource, SqlDataSource) or actual data objects such as DataTable, Collection of objects etc.

DataSourceID is the string identifier for .NET provided data source control and this property exists so that data-bound control and corresponding data source can be associated at the design time in markup. Internally, the control would look up for actual data source control using the id provided.



1. Make sure, you have not mentioned DataSourceID in the code behind. Something like-

Gridview1.DataSourceID="ds";


2. Try-

GridView1.DataSource = ds.Table[0];


3. Try-

Gridview1.DataSourceID = null;
GridView1.DataSource = ds.Table[0];
GridView1.DataBind();



Hopefully, one of the above solution/workaround works for you :)


Remove the DataSourceID attribute from the

<asp:GridView ID="GridView1" runat="server" DataSourceID="whatever">
        </asp:GridView>


这篇关于请解决错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆