我的数据库中有5个数据,这些数据我希望使用数据表在gridView中显示。但我的代码只显示GridView中的最后一个绑定数据?我的代码是。请指出错误?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand cmd = new SqlCommand("select Date from MusterRoll where EmpCode='"+code+"' and Month='1' and Year='2015'", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
var rows = ds.Tables[0].Rows;
foreach (DataRow row in rows)
{
string date1 = Convert.ToString(row["Date"]);
DateTime date2 = Convert.ToDateTime(date1);
SqlCommand cmd1 = new SqlCommand(" select TOP 1 m.EmpCode,m.NOH,m.OT,m.Late,m.Early,convert(varchar(10),m.Date,103)AS DATE,convert(varchar(10),s1.Shiftname,103)AS Shift From ShiftChange s,ShiftType s1,MusterRoll m WHERE s1.ShiftID=s.NShiftID and '" + date2 + "'>=Fromdate and Todate>='" + date2 + "' and m.Month = '1' and m.date='"+date2+"' and m.EmpCode='Neena' order by Todate desc", conn);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
sda1.Fill(dt);
//var rows1 = ds.Tables[0].Rows;
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
DataRow rpw = dt.Rows[rowIndex];
string EmpCode = rpw.Field<string>("EmpCode");
string NOH = rpw.Field<string>("NOH");
string OT = rpw.Field<string>("OT");
string Latae = rpw.Field<string>("Late");
string Early = rpw.Field<string>("Early");
string date3 =rpw.Field<string>("Date");
string Shift = rpw.Field<string>("Shift");
gvSingleemp.Visible = true;
gvSingleemp.DataSource = dt;
gvSingleemp.DataBind();
}
}
在我的shiftChange表中没有Field for date而不是我有fromDate和ToDate.I希望根据MusterRoll表日期显示员工shifft。因此,首先我选择了MusteRoll date nd checkrd,此日期存在于ShiftChange FromDate和ToDate之间(如果存在显示Shift
)答案 0 :(得分:3)
您正在循环中对GridView
进行数据绑定。您不需要循环,只需将其绑定到DataTable
:
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
sda1.Fill(dt);
gvSingleemp.DataSource = dt;
gvSingleemp.DataBind();
我仍然认为你根本不需要那些循环。我想您要选择EmpDetails
和EmpCode = code
所在的MusterRoll.Month='1' and MusterRoll.Year='2015'
所有记录。然后,您只需要一个SQL查询来填充一个DataTable
,它可以用作gvSingleemp
的数据源。那是对的吗?
如果是这样,这应该有效(注意我使用using
语句和sql参数):
DataTable tblData = new DataTable();
string sql = @"SELECT ed.EmpCode,ed.Name,ed.Age,ed.Date
FROM MusterRoll mr
INNER JOIN EmpDetails ed
ON mr.Date = ed.Date
WHERE mr.EmpCode=@EmpCode AND mr.Month=1 AND mr.Year=2015";
using(var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
using(var sda = new SqlDataAdapter(sql, conn))
{
var codeParam = new SqlParameter("@EmpCode", SqlDbType.VarChar).Value = code; // change type accordingly
sda.SelectCommand.Parameters.Add(codeParam);
sda.Fill(tblData); // no need for conn.Open/Close with SqlDataAdapter.Fill
}
gvSingleemp.Visible = true;
gvSingleemp.DataSource = tblData;
如果您不想加入表格,也可以使用EXISTS
:
string sql = @"SELECT ed.EmpCode, ed.Name, ed.Age, ed.Date
FROM EmpDetails ed
WHERE EXISTS
(
SELECT 1 FROM MusterRoll mr
WHERE mr.EmpCode = @EmpCode
AND mr.Month = 1 AND mr.Year=2015
AND mr.Date = ed.Date
)";
答案 1 :(得分:1)
您不必使用循环将DT绑定到GridView:
SqlCommand cmd1 = new SqlCommand(" select EmpCode,Name,Date,Age from EmpDetails where CompanyID='1'", conn);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
sda1.Fill(dt);
gvSingleemp.DataSource =dt;
gvSingleemp.DataBind();
答案 2 :(得分:0)
SqlDataAdapter Da = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
Da.Fill(data);
gvSingleemp.DataSource = data;
gvSingleemp.DataBind();