Click here to Skip to main content
16,021,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
private void CheckSerialNumber()
    {
        
        string strSNExists = "";
        openConnection();
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select Count(*) from AssetManagement where SerialNumber = @SerialNumber";
        cmd.Parameters.Add("@SerialNumber", MySqlDbType.VarChar).Value = txtSN.Text;
        MySqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            strSNExists = dr.GetChar(strSN).ToString();
        }
        closeConnection();
        dr.Close();

        if (strSNExists == "")
        {
            FileUpload img = (FileUpload)FileUpload1;
            Byte[] imgByte = null;
            if (img.HasFile && img.PostedFile != null)
            {
                HttpPostedFile File = FileUpload1.PostedFile;
                imgByte = new Byte[File.ContentLength];
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }
            openConnection();
            MySqlCommand cmdReg = new MySqlCommand();
            cmdReg.Connection = cn;
            cmdReg.CommandType = CommandType.Text;
            cmdReg.CommandText = "Insert into AssetManagement (Asset, Component, SerialNumber, Location, UsedBy, ProductPic) values (@Asset, @Component, @SerialNumber, @Location, @UsedBy, @ProductPic)";
            cmdReg.Parameters.Add("@Asset", MySqlDbType.VarChar).Value = txtAsset.Text;
            cmdReg.Parameters.Add("@Component", MySqlDbType.VarChar).Value = txtComponent.Text;
            cmdReg.Parameters.Add("@SerialNumber", MySqlDbType.VarChar).Value = txtSN.Text;
            cmdReg.Parameters.Add("@Location", MySqlDbType.VarChar).Value = txtLocation.Text;
            cmdReg.Parameters.Add("@UsedBy", MySqlDbType.VarChar).Value = txtUsed.Text;
            cmdReg.Parameters.Add("@ProductPic", MySqlDbType.VarBinary).Value = imgByte;
            cmdReg.ExecuteNonQuery();
            closeConnection();
            lblSNExists.Visible = false;
        }
        else
        {
            lblSNExists.Visible = true;
            txtSN.Focus();
        }
Posted
Updated 10-Aug-11 19:13pm
v2
Comments
walterhevedeich 11-Aug-11 1:14am    
I can see that the title may be a question, but I think the code already answered it. :)

Do you have problems with the code? Error?
BobJanova 11-Aug-11 5:56am    
What is the question here?

try this query

SQL
if exists(select * from AssetManagement where SerialNumber = @SerialNumber)
begin
print 'Exists'
//Your Code
end

else
begin
print 'Not Exists'
// Your Code
end
 
Share this answer
 
Comments
thejowker 11-Aug-11 1:28am    
where should i put that? please guide me i'm just a newbie
thejowker 11-Aug-11 1:48am    
its an error... what code is that?
Your existing code already checks that logic for existing serial number.




cmd.CommandText = "select Count(*) from AssetManagement where SerialNumber = @SerialNumber";
cmd.Parameters.Add("@SerialNumber", MySqlDbType.VarChar).Value = txtSN.Text;
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
strSNExists = dr.GetChar(strSN).ToString();
}
closeConnection();
dr.Close();

if (strSNExists == "")
{



This part in your code checks the logic
 
Share this answer
 
v3

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