简体   繁体   中英

system.data.sqlclient.sqlException on ExecuteNonQuery()

I am trying to add new patient to my database in my clinic management project using this method:

public void create_Patient()
{
    SqlCommand c = new SqlCommand();
    c.CommandText = "EXECUTE SP_add_patient @ownerID, @ownername, @petname, @animaltype, @animalbirthyear, @owneradress, @ownerphone";
    c.Parameters.AddWithValue("@ownerID", this.ownerID.ToString());
    c.Parameters.AddWithValue("@ownername", this.ownername.ToString());
    c.Parameters.AddWithValue("@petname", this.petname.ToString());
    c.Parameters.AddWithValue("@animaltype", this.animalType.ToString());
    c.Parameters.AddWithValue("@animalbirthyear", this.animalBirthYear.ToString());
    c.Parameters.AddWithValue("@owneradress", this.ownerAddress.ToString());
    c.Parameters.AddWithValue("@ownerphone", this.ownerPhone.ToString());
    c.Parameters.AddWithValue("@owneremail", this.ownerEmail.ToString());

    SQL_CON SC = new SQL_CON();
    SC.execute_non_query(c);
}

I'm using the stored procedure:

CREATE PROCEDURE dbo.SP_add_patient
    @ownerID varchar(100)
    , @ownername varchar(100)
    , @petname varchar(100)
    , @animaltype varchar(100)
    , @animalbirthyear int
    , @owneradress varchar(100)
    , @ownerphone varchar(100)
    , @owneremail varchar(100) AS 
INSERT INTO dbo.patientValues (@ownerID
                              ,@ownername
                              ,@petname
                              ,@animaltype
                              ,@animalbirthyear
                              ,@owneradress
                              ,@ownerphone
                              ,@owneremail);

and in execute_non_query(c) I am using ExecuteNonQuery() :

public void execute_non_query(SqlCommand cmd)
{

    try
    {
        // open a connection object
        this.conn.Open();
        cmd.Connection = conn;
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        MessageBox.Show("error", MessageBoxButtons.OK);
    }
    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }
}

When I get to ExecuteNonQuery() , I get an exception:

system.data.sqlclient.sqlException exception.

I looked a for a solution and tried to change the identity specification, but the primary key type is varchar , and I can't change it to a numeric type (the primary key must be a name).

How can I solve it?

You've got a bug here:

    catch (Exception ex)
    {
        MessageBox.Show("error", MessageBoxButtons.OK);
    }

should be something like:

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, MessageBoxButtons.OK);
    }

You can try it as below,

try
{
    SqlTransaction trx = null;
    connection = new SqlConnection(connectionString);
    connection.Open();
    trx = connection.BeginTransaction();
    
    SqlCommand c = new SqlCommand("[dbo].[SP_add_patient]", connection, trx);
    
    c.CommandType = CommandType.StoredProcedure;
    c.Parameters.AddWithValue(""@ownerID", this.ownerID.ToString());

    c.ExecuteNonQuery();
    trx.Commit();
    connection.Close();

}
catch (SqlException e)
{
                
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM