Hello Forum,
How to deducting Unit amount from wallet table.
I have a wallet Table like this
I want a situation where if there is more than 0 amount; and the amount is also above 50 then an event should happen. If the amount is 0 and less than 50, then it should redirect the user or a pop up message that will tell the user to add units to the wallet that the current unit is 0 and less than 50.
Also, the wallet belongs to one person which is the Admin. So if any other user who is linked and under the Admin is logged in and clicks the button there should be a deduction and an update in the “amount” column in the Admin’s wallet. For example if there is Admin (simons) and under this Admin, there are Users (benny, Chime and mike). If any of benny, Chime or mike clicks the button, then there should be a deduction in simons wallet (that is if simons has more than 0 and 50 amount in the wallet). I tried something in the code with conditional statement and it is working when deducting and updating the “amount” column in the wallet, but when there is 0 amount it still goes on to generate a QR code.
Here is what I tried:
protected void Button1_Click(object sender, EventArgs e)
{
// Read the file and convert it to Byte Array
string email = string.Empty;
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = string.Empty;
int size = FileUpload1.FileBytes.Length;
//Set the contenttype
if (ext == ".jpg" || ext == ".png")
{
contenttype = "image/jpg";
}
if (contenttype != string.Empty)
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
string sql = "INSERT INTO CardTbl(email, Name, Contyp, Size, image, Uid, CreatedBy, Role, CreatedDate, Organization)" +
" VALUES(@email, @Name, @ContentType, @Size, @image, @Uid, @CreatedBy, @Role, @CreatedDate, @Organization); SELECT @@IDENTITY";
SqlParameter[] parameters = new[]
{
new SqlParameter("@email", user.Text.Trim()),
new SqlParameter("@Name",filename ),
new SqlParameter("@ContentType",contenttype ),
new SqlParameter("@Size",size ),
new SqlParameter("@image",bytes ),
new SqlParameter("@Uid", labelid.Text.Trim()),
new SqlParameter("@CreatedBy", createby.Text.Trim()),
new SqlParameter("@Role", role.Text.Trim()),
new SqlParameter("@CreatedDate", DateTime.Now),
new SqlParameter("@Organization", named.Text.Trim())
};
int id = ExecuteScalar(sql, parameters);
Session["CardId"] = id;
if (id != -1)
{
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File Uploaded Successfully";
// Return a QR pic
BindCardAndQR();
UpdateWallet();
Response.Redirect("AuthenticateID.aspx");
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File Uploaded Failed";
}
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File format not recognised." + "JPG/PNG Formats ONLY";
}
}
private void UpdateWallet()
{
string email = string.Empty;
SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
string query = "";
if (AccountExist(email))
{
query = "UPDATE UserWallet SET amount -= 50 WHERE email=@email";
}
var cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@email", createby.Text.Trim());
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
private bool AccountExist(string email)
{
SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
string query = "SELECT COUNT(*) FROM UserWallet AS w INNER JOIN Users AS u ON w.email = u.CreatedBy WHERE u.Uid = '" + Session["user"] + "'";
var cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
int count = (int)cmd.ExecuteScalar();
return count > 0;
}