简体   繁体   中英

Return a value after inserting record into sql db

I'm entering some values into textboxes and want to save these values to my sql db. For now I just want to display a return value from my SQL StoredProcedure in a MessageBox to show whether the insert was a success.

Submit button code:

private void btnAdd_Click(object sender, RoutedEventArgs e)
{            
    if (txtType.Text.Length > 0 && txtNumber.Text.Length > 0 && txtLine1.Text.Length > 0)
    {
        string sOut = "";

        DataManager.SaveAddendum(txtType.Text, txtNumber.Text, txtLine1.Text, txtLine2.Text, txtLine3.Text, txtLine4.Text, sOut);

        MessageBox.Show(sOut);                
    }
}

Data management class called by the submit button:

public static void SaveAddendum(string sType, string sNumber, string sLine1, string sLine2, string sLine3, string sLine4, string sOut)
{
    // Create connection
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ToString());
    // Create command
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "sp_Insert_Addendum";
    // Create and add input parameters
    cmd.Parameters.AddWithValue("@Type", sType);            
    cmd.Parameters.AddWithValue("@Number", sNumber);
    cmd.Parameters.AddWithValue("@Line1", sLine1);
    cmd.Parameters.AddWithValue("@Line2", sLine2);
    cmd.Parameters.AddWithValue("@Line3", sLine3);
    cmd.Parameters.AddWithValue("@Line4", sLine4);

    // Attach connection to command
    cmd.Connection = con;
    // Open connection
    con.Open();
    // Execute command
    //cmd.ExecuteNonQuery();
    sOut = cmd.ExecuteScalar().ToString();            
    // Close connection
    con.Close();
    // Dispose connection
    con.Dispose();
}

Stored procedure:

ALTER PROCEDURE [dbo].[sp_Insert_Addendum]
-- Add the parameters for the stored procedure here
@Type varchar(3),
@Number varchar(4),
@Line1 varchar(60),
@Line2 varchar(60),
@Line3 varchar(60),
@Line4 varchar(60)
AS
BEGIN   
SET NOCOUNT ON;
BEGIN

    IF EXISTS(SELECT Addendum_Type, Addendum_Number FROM AddendumMst WHERE Addendum_Type = @Type AND Addendum_Number = @Number)
    BEGIN           
        --PRINT N'The addendum already exists.
        SELECT 'Already Exists'
    END
    ELSE
    BEGIN
        INSERT INTO AddendumMst (Addendum_Type, Addendum_Number, Line1, Line2, Line3, Line4) VALUES (@Type, @Number, @Line1, @Line2, @Line3, @Line4)        
        IF @@ERROR <> 0 
        BEGIN               
            --PRINT N'An error occurred inserting the addendum information.';
            SELECT 'Error inserting'
        END
        ELSE
        BEGIN               
            --PRINT N'The addendum has been inserted.';             
            SELECT 'Success'
        END;
    END 
END
END

My problem - the Messagebox displays a blank.

You're SaveAddendum mthod doesn't return a value. sOut is local to the method. Change your method to return a value (a string), and then you can display it in the MessageBox. Also, you can remove the sOut, as that isn't needed (and won't do what you're thinking it will unless you declare it as an out parameter):

public static string SaveAddendum(string sType, string sNumber, string sLine1, string sLine2, string sLine3, string sLine4)
{

    // Your code

    return sOut;
}

Then when you call the method, you can do something like this:

string sOut = DataManager.SaveAddendum(txtType.Text, txtNumber.Text, txtLine1.Text, txtLine2.Text, txtLine3.Text, txtLine4.Text);

MessageBox.Show(sOut); 

You should declare the sOut parameter as an output one:

public static void SaveAddendum(string sType, string sNumber, string sLine1, string sLine2, string sLine3, string sLine4, out string sOut)

and call it like this:

DataManager.SaveAddendum(txtType.Text, txtNumber.Text, txtLine1.Text, txtLine2.Text, txtLine3.Text, txtLine4.Text, out sOut);

Unrelated to the problem, I would also suggest to use using to handle the closing of the connection:

using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ToString())) {

    // . . . code

}

where the } replaces

con.Close();
con.Dispose();

The code is shorter and it guarantees that the connection is properly closed even if there is an exception.

I think you should mark parameter sOut in SaveAddendum procedure with "ref". Otherwise method doesnt know it should return value assigned to this parameter. But much better design would be to return this parameter from SaveAddendum, instead of having it return void.

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