在一次操作中插入值后从表中获取MAX值

时间:2017-11-23 08:06:20

标签: c# asp.net .net sql-server

我有一个让我头疼的问题。我需要你的帮助或建议来避免这个问题。

我有两个表“看附件”第一个表是Vacation表,第二个表是Request表。我使用Req_ID将Vacation表与Request表相关联。当我在Request表中插入值时,值会完美地插入到Requset表中。但是当我想在插入值后从Request表中获得MAX(Req_ID)时,我得到“0”。我需要Req_ID来连接Vacation表。

请查看代码以帮助我解决问题所在:

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 ";
        }
    }
}


如果您需要更多解释,请告诉我。

1 个答案:

答案 0 :(得分:0)

你必须从dt获得价值。为什么:

Convert.ToInt32(lblReq.Text);

lblReq.Text包含文字。

试试这个:

private void GetMax()
{
    d = "select MAX(Req_ID) as ID from Request";
    d = lblReq.Text;
    DataTable dt = db.getTable(d);
    if (dt != null && dt.Rows.Count > 0)
    {
        R = Convert.ToInt32(dt.Rows[0]["ID"].ToString());

    }
}