Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
how to find total number of rows inserted in result set


HTML
    protected void Button1_Click(object sender, EventArgs e)
    {
       if(TextBox.Text !=String.Empty)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                string textBoxText = ((TextBox)row.FindControl("TextBox1")).Text;
                cmd = new SqlCommand("Insert into Text(number)values(@number)", con);
                cmd.Parameters.Add(new SqlParameter("@number", textBoxText));
                con.Open();
                int a = cmd.ExecuteNonQuery();
                con.Close();
                ShowAlertMessage("inserted");
                TextBox.Text = "";


            }

        }
else
        {
             ShowAlertMessage("Enter the value");

        }
    }
Posted
Comments
King Fisher 13-Dec-14 2:53am    
Use Increment Operator within that loop
Maciej Los 13-Dec-14 4:23am    
What's database?
Rajalakshmi Ravi 14-Dec-14 23:39pm    
sry for delay of response i am using sql server 2008

I see several issues, like SQLserver stop responding or application may hang-up.

Rather then inserting value one-by-one, use stored procedure (SP)[^], like:
SQL
CREATE PROCEDURE usp_AddValue
    @val INT
AS
BEGIN
   INSERT INTO TableName (FieldName)
   VALUES(@val)
   --return number of rows affected
   RETURN @@ROWCOUNT
END

Why to use SP? Becasue of SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Security: New SQL Truncation Attacks And How To Avoid Them[^]
SQL Injection and how to avoid it[^]
@@ROWCOUNT (T-SQL)[^]
Walkthrough: Displaying Data Using a Stored Procedure in the GridView Web Server Control[^]
How to: Execute a Stored Procedure that Returns Rows[^]
How to: Execute a Stored Procedure that Returns a Single Value[^]

I have no idea why do you loop through the collection of rows. Sounds, like you wanted to update data instead of adding them. In that case, use SP:
SQL
CREATE PROCEDURE usp_UpdateAll
    @val INT
AS
BEGIN
   UPDATE TableName SET FieldName = @val
   --return number of rows affected
   RETURN @@ROWCOUNT
END


If you would like to update single record:
SQL
CREATE PROCEDURE usp_UpdateSingle
    @id INT,
    @val INT
AS
BEGIN
   UPDATE TableName SET FieldName = @val
   WHERE Key = @id
   --return number of rows affected
   RETURN @@ROWCOUNT
END


Good luck!
 
Share this answer
 
Comments
DamithSL 13-Dec-14 5:51am    
voted 4, nice set of resources which will surely help
Maciej Los 13-Dec-14 6:04am    
Thank you ;)
I appreciate valuable discussion ;)
DamithSL 13-Dec-14 6:10am    
people like you, make this place better, Thank you and keep up the good work.
Maciej Los 13-Dec-14 6:48am    
In Poland we say: And vice versa ;)
C#
int insertCount =0;
con.Open();
foreach (GridViewRow row in GridView1.Rows)
{
    string textBoxText = ((TextBox)row.FindControl("TextBox1")).Text;
    cmd = new SqlCommand("Insert into Text(number)values(@number)", con);
    cmd.Parameters.AddWithValue("@number", textBoxText);
    int a = cmd.ExecuteNonQuery();
    if(a>0)
       insertCount++;
    TextBox.Text = "";

}
con.Close();
ShowAlertMessage(string.Format("{0} records inserted", insertCount));
 
Share this answer
 
v3
Comments
Maciej Los 13-Dec-14 4:41am    
My vote of 3. Although your code is helpful, but i see several reasons to not use it.
Please, see my answer.
DamithSL 13-Dec-14 4:51am    
OP using Parameters, it is safe.
I have used the same connection for all the inserts and it will prevent multiple connections.
I don't see considerable improvement in your code other than additional things like update which OP newer ask the question.
even for using your SP, OP have to insert one by one, you are not sending array or list of items at ones.
Maciej Los 13-Dec-14 5:25am    
OK, you can test it on your database. Try to add via Web (in loop) over 10K records. Let me know about application stability... Does it hanged up or not?
That's what i'm talking about.
DamithSL 13-Dec-14 5:30am    
Issue is not with inserting but loading 10k records to list view and displaying all of them!
Maciej Los 13-Dec-14 5:35am    
With inserting and loading...
If you don't believe me, try... Even if the sql server is a local server, application may hang up.
Respectfully,
Maciej

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