【发布时间】:2017-11-23 08:06:20
【问题描述】:
我有一个让我头疼的问题。我需要您的帮助或建议来避免此问题。
我有两个表“见附件”第一个表是假期表,第二个表是请求表。我已使用 Req_ID 将假期表与请求表链接起来。当我将值插入到请求表中时,这些值会完美地插入到请求集表中。但是当我想在插入值后从请求表中获取 MAX(Req_ID) 时,我得到“0”。我需要 Req_ID 来链接假期表。
请查看代码以帮助我解决问题所在:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Vavation : System.Web.UI.Page
{
Dbclass db = new Dbclass();
string s = "";
string d = "";
int R;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if(!IsPostBack)
{
tbid.Text = Session["uid"].ToString();
lblDept.Text = Session["Did"].ToString();
lblid.Text = tbid.Text;
string s = "Select * From Employee where National_ID='" + lblid.Text + "'";
DataTable dt = db.getTable(s);
if (dt != null && dt.Rows.Count > 0)
{
tbid.Text = dt.Rows[0]["National_ID"].ToString();
tbname.Text = dt.Rows[0]["FirstName"].ToString() + ' ' + dt.Rows[0]["LastName"].ToString();
lblDept.Text = dt.Rows[0]["Dept_ID"].ToString();
}
string d = "SELECT Balance FROM Vacation WHERE (National_ID = '" + lblid.Text + "') AND (StartDate =(SELECT MAX(StartDate) FROM Vacation WHERE (National_ID ='" + lblid.Text + "'))) AND (Balance = (SELECT MIN(Balance) FROM Vacation WHERE (National_ID ='" + lblid.Text + "')))";
DataTable t = db.getTable(d);
if (t != null && t.Rows.Count > 0)
{
lbldays.Text = t.Rows[0]["Balance"].ToString();
}
}
}
catch(Exception ex)
{
lberr.Text = ex.Message;
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
s = "Insert into Request (National_ID, Type_Req, Dept_ID,Date) values ('" + lblid.Text + "', 'Vacation','" + lblDept.Text + "', '"+ DateTime.Now +"')";
if (db.Run(s))
{
}
GetMax();
AddVaction();
}
private void GetMax()
{
d = "select MAX(Req_ID) from Request";
d = lblReq.Text;
DataTable dt = db.getTable(d);
if (dt != null && dt.Rows.Count > 0)
{
R = Convert.ToInt32(lblReq.Text);
}
}
private void AddVaction()
{
int Remaine = Convert.ToInt32(lbldays.Text);
DateTime start = DateTime.Parse(tbstartvaca.Text).Date;
if (Remaine <= 30 && Remaine >= 0 && start >= DateTime.Now)
{
TimeSpan remaindate;
DateTime end = DateTime.Parse(tbendvaca.Text).Date;
TimeSpan vacation = TimeSpan.Parse(lbldays.Text);
int Balance = (int)vacation.TotalDays;
TimeSpan total;
if (start > end)
{
lberr.Text = "Please check again on Starting date";
return;
}
remaindate = end - start;
int days = (int)remaindate.TotalDays;
lberr.Text = "you have left with " + remaindate.TotalDays + "days.";
total = vacation - remaindate;
int T = (int)total.TotalDays;
lbldays.Text = "you have left with " + total.TotalDays + "days.";
s += "Insert into Vacation (Balance, National_ID, NoOfDays, StartDate, EndDate, Dept_ID, Req_date, Req_ID) values ('" + T + "', '" + lblid.Text + "', '" + days + "', '" + tbstartvaca.Text + "', '" + tbendvaca.Text + "', '" + lblDept.Text + "', '" + DateTime.Now + "', '" + R + "')";
{
//lberr.Text = "The data has update";
}
}
else
{
lberr.Text = "You Don't a Balance of Vacation OR Start of Vacation is less than date of day ";
}
}
}
如果您需要更多解释,请告诉我。
【问题讨论】:
-
d = "select MAX(Req_ID) from Request"; d = lblReq.Text;没有多大意义…… -
这里好像有无意的代码。 d = "从请求中选择 MAX(Req_ID)"; d = lblReq.Text; // 为什么是这一行?
-
顺便说一句,使用具有非描述性名称的全局定义变量来存储几乎所有内容并不能真正实现清晰的代码。即使您会执行您的
Max查询(您不这样做),您也不会对结果做任何事情。完全不清楚您要完成什么。
标签: c# asp.net .net sql-server