I have created a table in sql server, an insert stored procedure, a aspnet webform with 5 text boxes to edit the parameter values, a label to display the output identity value from scope identity and another label to display the inserted or not inserted report from the execution of the ExecuteNonQuery() command.
I don’t know the code to display the output identity returned by scope identity function, so please, after examining my code bellow, supply me the corrections and the aspnet code that will display the identity in my label.
I am using web developer express 2010 and sql server express 2008.
My goal is to insert aspnet text boxes data into the table using the stored procedure. I also want to display in a label the output identity returned by scope identity function in the stored procedure.
I have tried several times the aspnet code below, but nothing seems to be happening. If I use the stored procedure in a sql management studio window it just works fine.
The code is bellow:
The table name in sql server is Students and the table definition is as follow:
StudentID int IDENTITY(1,1) NOT NULL,
LastName varchar(35) NOT NULL,
MiddleName varchar(35) NOT NULL,
FirstName varchar(35) NOT NULL,
FothersCompleteName varchar(65) NOT NULL,
MothersCompleteName varchar(65) NOT NULL,
The stored procedure is as follow:
CREATE PROCEDURE dbo.InsertStudentsNames
@LastName varchar(35),
@MiddleName varchar(35),
@FirstName varchar(35),
@FothersCompleteName varchar(65),
@MothersCompleteName varchar(65),
@IdentitY int OUTPUT
AS
BEGIN
INSERT Students (LastName, MiddleName, FirstName, FothersCompleteName, MothersCompleteName)
VALUES
(@LastName, @MiddleName, @FirstName, @FothersCompleteName, @MothersCompleteName)
END
SELECT @Identity = SCOPE_IDENTITY();
RETURN @Identity
I have defined 5 textboxs, two labels and a button in aspnet webform as above:
<asp:TextBox ID="lastNameTextBox" runat="server" Width="232px"></asp:TextBox>
<asp:TextBox ID="middleNameTextBox" runat="server" Width="232px"></asp:TextBox>
<asp:TextBox ID="firstNameTextBox" runat="server" Width="232px"></asp:TextBox>
<asp:TextBox ID="fothersCompleteNameTextBox" runat="server" Width="232px"></asp:TextBox>
<asp:TextBox ID="mothersCompleteNameTextBox" runat="server" Width="232px"></asp:TextBox>
<asp:Label ID="identityLabel" runat="server" Text="LastName:"></asp:Label>
<asp:Label ID="insertReportLabel" runat="server" Text="LastName:"></asp:Label>
<asp:Button ID="insertStudent" runat="server" onclick=" insertStudentButton_Click "
Text="insert" />
I did not put the code to display the identity value from the database because I don’t know it. Please supply me. But the insertStudentButton_Click event contains the following code:
protected void insertStudentButton_Click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=86.25.18.45\\SQLEXPRESS;Initial Catalog=Students;User ID=myusername;Password=mypassword");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = " InsertStudentsNames ";
cmd.Parameters.Add("@LastName", SqlDbType.Int).Value = lastNameTextBox.Text.Trim();
cmd.Parameters.Add("@MiddleName", SqlDbType.Int).Value = middleNameTextBox.Text.Trim();
cmd.Parameters.Add("@FirstName", SqlDbType.Int).Value = firstNameTextBox.Text.Trim();
cmd.Parameters.Add("@FothersCompleteName", SqlDbType.Int).Value = fothersCompleteNameTextBox.Text.Trim();
cmd.Parameters.Add("@MothersCompleteName", SqlDbType.Int).Value = mothersCompleteNameTextBox.Text.Trim();
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
insertReportLabel.Text = "Student inserted sucessfully";
}
catch
{
insertReportLabel.Text = "Student NOT inserted";
}
finally
{
con.Close();
con.Dispose();
}
}
When I click the button, it displays in the label the fallowing text: Student NOT inserted.