Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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 follows:-
      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,
<pre>
The stored procedure is as follow:
<pre>
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 below:-
      <asp:TextBox ID="lastNameTextBox" runat="server" Width="232px">
      <asp:TextBox ID="middleNameTextBox" runat="server" Width="232px">
 <asp:TextBox ID="firstNameTextBox" runat="server" Width="232px">
        <asp:TextBox ID="fothersCompleteNameTextBox" runat="server" Width="232px">
        <asp:TextBox ID="mothersCompleteNameTextBox" runat="server" Width="232px">

<asp:Label ID="identityLabel" runat="server" Text="LastName:">
<asp:Label ID="insertReportLabel" runat="server" Text="LastName:">

<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.
Posted
Updated 14-Feb-12 6:40am
v2
Comments
Varun Sareen 14-Feb-12 12:40pm    
Edit for: added pre tag

1 solution

If you caught the actual Exception being thrown it would give you a better idea what the problem is.

It is pointless to have an empty catch block. You implement exception handling for a reason, and that reason is not for program flow.

On the plus side you are using stored procs and parameterized sql commands. You are far ahead of most who post here. Well done.
 
Share this answer
 
v2
Comments
Member 10380351 28-Nov-14 5:46am    
can you send me the code how to update sql table using stroed procuder

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