IListSource不包含任何数据源. [英] IListSource does not contain any data sources.
本文介绍了IListSource不包含任何数据源.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码在此行给出了以上错误( DataGrid1.DataBind(); )
My code code giving above error on this line( DataGrid1.DataBind();)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection();
string strPath = string.Empty;
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
con.Close();
DataGrid1.Visible = true;
Label1.Visible = true;
Label1.Text = "Data Displayed Successfully";
}
}
protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
int i = 0;
bool blnSelect = false;
foreach (GridViewRow row in DataGrid1.Rows)
{
CheckBox rb = (CheckBox)row.FindControl("RowSelector");
Label lbltagcode = (Label)row.FindControl("lbltagcode");
Label lblParent = (Label)row.FindControl("lblParent");
Label lblCategory_code = (Label)row.FindControl("lblCategory_code");
if (rb.Checked)
{
SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
con.Close();
DataGrid1.Visible = true;
Label1.Visible = true;
Label1.Text = "Data Inserted Successfully";
}
rb.Checked = false;
i++;
}
}
}
推荐答案
完成了
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection();
string strPath = string.Empty;
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
con.Close();
DataGrid1.Visible = true;
Label1.Visible = true;
Label1.Text = "Data Displayed Successfully";
}
}
protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
int i = 0;
bool blnSelect = false;
foreach (GridViewRow row in DataGrid1.Rows)
{
CheckBox rb = (CheckBox)row.FindControl("RowSelector");
Label lbltagcode = (Label)row.FindControl("lbltagcode");
TextBox Rmcode = (TextBox)DataGrid1.FooterRow.FindControl("txtRmcode");
if (rb.Checked)
{
cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@Sub_code", SqlDbType.VarChar).Value = lbltagcode.Text.ToString().Trim();
cmd.Parameters.AddWithValue("@rm_code", SqlDbType.VarChar).Value = Rmcode.Text.ToString();
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
cmd.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text = "Data Inserted Successfully";
}
rb.Checked = false;
i++;
}
con.Close();
}
}
您的buttonSave点击事件正在更新数据而不是获取数据.
因此,以下代码不正确:
Your buttonSave click event is updating data and not fetching it.
Hence the following code is not correct:
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
如果要再次获取数据,则需要重新绑定网格.
You need to rebind grid if you are fetching data again.
这篇关于IListSource不包含任何数据源.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文