Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

help about error handler if values is existing in database(login) in c#

imba23

Professional
Advanced Member
Messages
165
Reaction score
0
Points
26
hello po mga sir, gumawa po ako ng error handler na kapag may existing user may lalabas na error, pero lumalabas yung error message at yung DialogResult parehas, ang gusto ko po sanang mangyari, ichecheck nya muna yung values if existing then lalabas yung error message, if hindi naman existing lalabas yung save message pano po ba magagawa yun?

eto po yung code ko sa save:
Code:
usercheck();

        DialogResult dr = MetroMessageBox.Show(this, "Are you sure you want to Save without your desire Sales Count or Sales Amount?", "Wait!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

        if (txtCount.Text == "0" || txtAmount.Text == "0")
        {
            if (dr == DialogResult.Yes)
            {

                Data.con.Open();



                string SaveStr = "Insert into dbinfo.tbluser (UserID, UserName, UserPassword, UserLevel, TargetCount, TargetAmount) Values (@UserID, @UserName, @UserPassword, @UserLevel, @TargetCount, @TargetAmount)";
                MySqlCommand SaveCmd = new MySqlCommand(SaveStr, Data.con);

                SaveCmd.Parameters.AddWithValue("@UserID", txtID.Text);
                SaveCmd.Parameters.AddWithValue("@UserName", txtName.Text);
                SaveCmd.Parameters.AddWithValue("@UserPassword", txtPassword.Text);
                SaveCmd.Parameters.AddWithValue("@UserLevel", cbLevel.Text);
                SaveCmd.Parameters.AddWithValue("@TargetCount", txtCount.Text);
                SaveCmd.Parameters.AddWithValue("@TargetAmount", txtAmount.Text);

                SaveCmd.ExecuteNonQuery();
                Data.con.Close();
                LoadData();

                MetroMessageBox.Show(this, "User Entry Saved!", "Saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);


                clear();

            }
            else if (dr == DialogResult.No)
            {
                clear();
            }
        }

tapos eto po yung code ko sa usercheck() method
Code:
string constring = "server=localhost;port=3306;username=root;password=root";
        string Query = "Select * from dbinfo.tbluser where UserName=@UserName";
        MySqlConnection con = new MySqlConnection(constring);
        MySqlCommand Check = new MySqlCommand(Query, con);


        Check.Parameters.AddWithValue("@UserName", this.txtName.Text);
        con.Open();
        MySqlDataReader dr = Check.ExecuteReader();
         while (dr.Read())
        {
            if (dr.HasRows)
            {
                MetroMessageBox.Show(this, "The User Name " + dr[1].ToString() + " Already exist!","Existing User", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Clear();                    
                break;

            }
            else
            {
                txtCheck.Visible = false;
            }
        }

Pano po aayusin to mga master? thank you po
 
Arrange mo lang coding mo ts,

1. Gawin mong boolean function ang usercheck() function mo, if may existing user returns false else kung wala naman returns true.
2. Sa command button mo naman dagdagan mo ng condition
If usercheck() = false{
return false;
}else{
saveEntry()
}
 
Tama si xloverboyx, gawin mo siyang boolean function type, then sa query mo instead na gumamit ka ng "Select * from" gawin mo nalang siyang "Select Count(*) from" then use ExecuteScalar method to retrieve the count of users information

Code:
int userExist = (int) sqlCommand.ExecuteScalar();
iif(UserExist > 0)
{
   //Username exist
}
else
{
   //Username doesn't exist.
}
 
I just added some code to yours..

Code:
try {
    string constring = "server=localhost;port=3306;username=root;password=root";
    string Query = "Select * from dbinfo.tbluser where UserName=@UserName";
    MySqlCommand Check = new MySqlCommand();
    MySqlDataReader dr = default(MySqlDataReader);
    using (MySqlConnection con = new MySqlConnection(constring)) {
        con.Open();
        Check = new MySqlCommand(Query, con);
        Check.Parameters.AddWithValue("@UserName", txtName.Text);
        dr = Check.ExecuteReader();
        if (dr.HasRows) {
            MetroMessageBox.Show(this, "The User Name " + txtName.Text.Trim + " Already exist!", "Existing User", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtName.Focus();

        } else {
                    'DITO MO INSERT YUNG INSERTING/SAIVNG NEW DATA TO YOUR DATABASE
        }
    }
} catch (Exception ex) {
    MessageBox.Show(ex.Message.ToString, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
 
Last edited:
you can add a primary key to username then use try catch block catch(SQLException)

try{
execute your code
}
catch(SQLException)
{
MessageBox.Show("Username already exist");
}
 
Up ko lang po, Okay na po pala, sariling code ko land din naka solve :lol:

eto po yung code ko medyo okay na:
Code:
private void SaveDetails()

        {
        string InsertNew = "Insert into dbinfo.tbluser (UserID, UserName, UserPassword, UserGender, UserLevel, TargetCount, TargetAmount) Values (@UserID, @UserName, @UserPassword, @UserGender, @UserLevel, @TargetCount, @TargetAmount)";
        string ValueExist = "Select count(*) from dbinfo.tbluser where UserName=@UserName";
                 

        MySqlConnection conn = new MySqlConnection("server=localhost;port=3306;username=root;password=root");

        MySqlCommand InsertCommand = new MySqlCommand(InsertNew, conn);
         InsertCommand.Parameters.AddWithValue("@UserID", txtID.Text);
         InsertCommand.Parameters.AddWithValue("@UserName", txtName.Text);
         InsertCommand.Parameters.AddWithValue("@UserPassword", txtPassword.Text);
         InsertCommand.Parameters.AddWithValue("@UserGender", cbGender.Text);
         InsertCommand.Parameters.AddWithValue("@UserLevel", cbLevel.Text);
         InsertCommand.Parameters.AddWithValue("@TargetCount", txtCount.Text);
         InsertCommand.Parameters.AddWithValue("@TargetAmount", txtAmount.Text);


         MySqlCommand QueryCommand = new MySqlCommand(ValueExist, conn);
        QueryCommand.Parameters.AddWithValue("@UserName", this.txtName.Text);

        MySqlDataReader InsertReader;

        conn.Open();
        if ((int)(long)QueryCommand.ExecuteScalar() >0)
        {
            MetroMessageBox.Show(this, "User Name '" + txtName.Text + "' Already exist!", "Existing User", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtName.Clear();
        }
        else
        {
            InsertReader = InsertCommand.ExecuteReader();
            LoadData();
            MetroMessageBox.Show(this, "User Entry Saved!", "Saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            while (InsertReader.Read())
            {

            }
        }
        conn.Close();
        
        }

sana makatulong din to sa iba :)
 
enclose your code on try catch error handler para hindi mag debug ang program mo if may ma encounter na error.
 
upon saving or pag pindot mo ng save

search mo muna
if exist then notify the user
exit sub

then continue your procedure sa saving
 
Back
Top Bottom