Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

please I need your help. I have a code which get max value from Request table and save the returned value into a variable. then use this variable with insert statement to save into Vacation table to use it when I want display the requests. Here is my code

C#
<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Vavation : System.Web.UI.Page
{
Dbclass db = new Dbclass();
string s = "";
string d = "";
int R;
string firstVariable = string.Empty;
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)
{
AddVaction();
GetMax();
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 + "')";
db.Run(s);

}
else
{
lberr.Text = "You Don't a Balance of Vacation OR Start of Vacation is less than date of day ";
}

}

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

}

}

private void AddVaction()
{
s = "Insert into Request (National_ID, Type_Req, Dept_ID,Date) values ('" + lblid.Text + "', 'Vacation','" + lblDept.Text + "', '" + DateTime.Now + "')";
db.Run(s);

}
}

when I run the code I get Max Req_ID "zero" although the code running with out errors and the Max Req_ID should be the latest value.

please anyone has idea to solve the problem help me. I do not where the problem is. is it in database structure or in the code.

What I have tried:

I tried many code but I could not get any value.
Posted
Updated 26-Nov-17 21:39pm

Don't worry about that for a moment - you have much more serious problems!
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And in a website? Where anyone in the world can get access? You have to fix this immediately throughout the whole of your software - or your DB (and the site it serves) will not last long...
 
Share this answer
 
Comments
Laxmidhar tatwa technologies 27-Nov-17 4:37am    
A good solution
Looking at that code, I'm pretty sure the problem is in the code.

I don't have a solution for you but will offer some practical advice which may help you find the solution:
• Your code is vulnerable to SQL injection attack - parameterize your inputs & don't rely on them to be safe to use in your SQL statement.
• Switch to using an ORM.
• Your methods are too "busy" - have each method do one thing only. It's better to have more simplistic methods than only a few really complex ones.
• Your method GetMax() is void - rather let it return the value instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900