I want to display all the data in gridview which i am inserting in runtime. For that i have written code but getting this error.
我想在gridview中显示我在运行时插入的所有数据。为此,我已编写代码,但收到此错误。
"Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."
“DataSource和DataSourceID都是在'GridView1'上定义的。删除一个定义。”
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select * from Items where ItemId='" + TxtItemId.Text + "'", con);
DataSet ds = new DataSet();
da.Fill(ds,"Items");
GridView1.DataSource = ds;
GridView1.DataBind();
}
Pls modify my code where is my mistake.
请修改我的代码在哪里是我的错误。
Thanks, Sumit
谢谢,Sumit
4 个解决方案
#1
1
The two properties are mutually exclusive (when you use one of them, you are not allowed to use the other one):
这两个属性是互斥的(当您使用其中一个属性时,不允许使用另一个属性):
- The DataSourceID property is to be used with a DataSource control (ObjectDataSource, XmlDataSource, SqlDataSource, etc)
- DataSourceID属性将与DataSource控件一起使用(ObjectDataSource,XmlDataSource,SqlDataSource等)
- The DataSource property is to be used with custom objects / custom data binding.
- DataSource属性将与自定义对象/自定义数据绑定一起使用。
Examples:
例子:
-
DataSourceId
DataSourceID的
@asp:ObjectDataSource ID="ods1" runat="server" SelectMethod="Test" TypeName="TestBL" /@ @asp:GridView ID="gv1" runat="server" DataSourceID="ods1" /@
@asp:ObjectDataSource ID =“ods1”runat =“server”SelectMethod =“Test”TypeName =“TestBL”/ @ @ asp:GridView ID =“gv1”runat =“server”DataSourceID =“ods1”/ @
in this case databinding occurs automatically
在这种情况下,数据绑定自动发生
-
DataSource
数据源
@asp:GridView ID="gv2" runat="server" %@
@asp:GridView ID =“gv2”runat =“server”%@
and in code behind you would have something like this:
并在代码后面你会有这样的事情:
overrides void OnLoad(..)
{
List<DataObject> source = new List<DataObject>();
source.Add(new DataObject(..));
source.Add(new DataObject(..));
source.Add(new DataObject(..));
gv2.DataSource = source;
gv2.DataBind();
}
Please note that I have used @ instead of angular brackets.
请注意,我使用了@而不是尖括号。
#2
0
It seems like you want to bind your datasource manually. Remove the DataSourceID property at the design view from your gridview. It should be something like that :
您似乎想要手动绑定数据源。从gridview中删除设计视图中的DataSourceID属性。它应该是这样的:
<asp:GridView runat="server" ID="grid" DataSourceID="SqlDataSource1"></asp:GridView>
Just remove the DataSourceID="SqlDataSource1" property.
只需删除DataSourceID =“SqlDataSource1”属性即可。
#3
0
You need to use SqlParameter with sql command.
您需要使用SqlParameter和sql命令。
and you are missing logic. First when form loads at that moment you need to bind the complete Item Table data to your Grid, Ten you would have a textbox and button where you want to filter the grid.
你缺少逻辑。首先,当表单加载时,你需要将完整的Item Table数据绑定到Grid,Ten你需要一个文本框和按钮来过滤网格。
Here is your Complete Code . Use it. 100% works.
这是您的完整代码。用它。 100%有效。
<div id = "dvFilterGrid">
Filter Grid : <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
<asp:Button ID="btnFilterGrid" runat="server" Text="Find"
onclick="btnFilterGrid_Click" />
</div>
and your code behind
和你的代码背后
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();
}
}
protected void BindGridData()
{
con.Open();
using (SqlCommand cmd = new SqlCommand("Select * from Items", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnFilterGrid_Click(object sender, EventArgs e)
{
int ItemId = Convert.ToInt32(txtFilter.Text.Trim());
FilterData(ItemId);
}
private void FilterData(int ItemId)
{
string query = "select * from Items where ItemId=@ItemId ";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.Add(new SqlParameter("@ItemId", ItemId));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"Items");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
#4
0
For inserting data on button click
用于在按钮单击上插入数据
SqlConnection con = new SqlConnection("connection string");
SqlDataAdapter myadp = new SqlDataAdapter();
myadp.InsertCommand = new SqlCommand();
myadp.InsertCommand.Connection = con;
con.Open();
myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
int i = myadp.InsertCommand.ExecuteNonQuery();
if (i == 1)
{
// Response.Write("<script>alert('number updated sucessfully')</script>");
Label2.Text = "Number updated sucessfully".ToString();
TextBox10.Text = "";
}
else
{
//Response.Write("<script>alert('somthing went wrong try again')</script>");
Label2.Text = "somthing went wrong try again".ToString();
}
con.Close();
Responce.Redirect("your page.aspx");
To display data on the grid view
在网格视图上显示数据
SqlCommand command = new SqlCommand("SELECT * from number");
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
so the overall coding will be
所以整体编码将是
SqlConnection con = new SqlConnection("connection string");
SqlDataAdapter myadp = new SqlDataAdapter();
myadp.InsertCommand = new SqlCommand();
myadp.InsertCommand.Connection = con;
con.Open();
myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
int i = myadp.InsertCommand.ExecuteNonQuery();
if (i == 1)
{
// Response.Write("<script>alert('number updated sucessfully')</script>");
Label2.Text = "Number updated sucessfully".ToString();
TextBox10.Text = "";
}
else
{
//Response.Write("<script>alert('somthing went wrong try again')</script>");
Label2.Text = "somthing went wrong try again".ToString();
}
con.Close();
Responce.Redirect("your page.aspx");
To display data on the grid view
SqlCommand command = new SqlCommand("SELECT * from number");
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
here the Responce.Redirect("your page.aspx"); will do the trick for you
#1
1
The two properties are mutually exclusive (when you use one of them, you are not allowed to use the other one):
这两个属性是互斥的(当您使用其中一个属性时,不允许使用另一个属性):
- The DataSourceID property is to be used with a DataSource control (ObjectDataSource, XmlDataSource, SqlDataSource, etc)
- DataSourceID属性将与DataSource控件一起使用(ObjectDataSource,XmlDataSource,SqlDataSource等)
- The DataSource property is to be used with custom objects / custom data binding.
- DataSource属性将与自定义对象/自定义数据绑定一起使用。
Examples:
例子:
-
DataSourceId
DataSourceID的
@asp:ObjectDataSource ID="ods1" runat="server" SelectMethod="Test" TypeName="TestBL" /@ @asp:GridView ID="gv1" runat="server" DataSourceID="ods1" /@
@asp:ObjectDataSource ID =“ods1”runat =“server”SelectMethod =“Test”TypeName =“TestBL”/ @ @ asp:GridView ID =“gv1”runat =“server”DataSourceID =“ods1”/ @
in this case databinding occurs automatically
在这种情况下,数据绑定自动发生
-
DataSource
数据源
@asp:GridView ID="gv2" runat="server" %@
@asp:GridView ID =“gv2”runat =“server”%@
and in code behind you would have something like this:
并在代码后面你会有这样的事情:
overrides void OnLoad(..)
{
List<DataObject> source = new List<DataObject>();
source.Add(new DataObject(..));
source.Add(new DataObject(..));
source.Add(new DataObject(..));
gv2.DataSource = source;
gv2.DataBind();
}
Please note that I have used @ instead of angular brackets.
请注意,我使用了@而不是尖括号。
#2
0
It seems like you want to bind your datasource manually. Remove the DataSourceID property at the design view from your gridview. It should be something like that :
您似乎想要手动绑定数据源。从gridview中删除设计视图中的DataSourceID属性。它应该是这样的:
<asp:GridView runat="server" ID="grid" DataSourceID="SqlDataSource1"></asp:GridView>
Just remove the DataSourceID="SqlDataSource1" property.
只需删除DataSourceID =“SqlDataSource1”属性即可。
#3
0
You need to use SqlParameter with sql command.
您需要使用SqlParameter和sql命令。
and you are missing logic. First when form loads at that moment you need to bind the complete Item Table data to your Grid, Ten you would have a textbox and button where you want to filter the grid.
你缺少逻辑。首先,当表单加载时,你需要将完整的Item Table数据绑定到Grid,Ten你需要一个文本框和按钮来过滤网格。
Here is your Complete Code . Use it. 100% works.
这是您的完整代码。用它。 100%有效。
<div id = "dvFilterGrid">
Filter Grid : <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
<asp:Button ID="btnFilterGrid" runat="server" Text="Find"
onclick="btnFilterGrid_Click" />
</div>
and your code behind
和你的代码背后
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();
}
}
protected void BindGridData()
{
con.Open();
using (SqlCommand cmd = new SqlCommand("Select * from Items", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnFilterGrid_Click(object sender, EventArgs e)
{
int ItemId = Convert.ToInt32(txtFilter.Text.Trim());
FilterData(ItemId);
}
private void FilterData(int ItemId)
{
string query = "select * from Items where ItemId=@ItemId ";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.Add(new SqlParameter("@ItemId", ItemId));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"Items");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
#4
0
For inserting data on button click
用于在按钮单击上插入数据
SqlConnection con = new SqlConnection("connection string");
SqlDataAdapter myadp = new SqlDataAdapter();
myadp.InsertCommand = new SqlCommand();
myadp.InsertCommand.Connection = con;
con.Open();
myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
int i = myadp.InsertCommand.ExecuteNonQuery();
if (i == 1)
{
// Response.Write("<script>alert('number updated sucessfully')</script>");
Label2.Text = "Number updated sucessfully".ToString();
TextBox10.Text = "";
}
else
{
//Response.Write("<script>alert('somthing went wrong try again')</script>");
Label2.Text = "somthing went wrong try again".ToString();
}
con.Close();
Responce.Redirect("your page.aspx");
To display data on the grid view
在网格视图上显示数据
SqlCommand command = new SqlCommand("SELECT * from number");
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
so the overall coding will be
所以整体编码将是
SqlConnection con = new SqlConnection("connection string");
SqlDataAdapter myadp = new SqlDataAdapter();
myadp.InsertCommand = new SqlCommand();
myadp.InsertCommand.Connection = con;
con.Open();
myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
int i = myadp.InsertCommand.ExecuteNonQuery();
if (i == 1)
{
// Response.Write("<script>alert('number updated sucessfully')</script>");
Label2.Text = "Number updated sucessfully".ToString();
TextBox10.Text = "";
}
else
{
//Response.Write("<script>alert('somthing went wrong try again')</script>");
Label2.Text = "somthing went wrong try again".ToString();
}
con.Close();
Responce.Redirect("your page.aspx");
To display data on the grid view
SqlCommand command = new SqlCommand("SELECT * from number");
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
here the Responce.Redirect("your page.aspx"); will do the trick for you