Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I want to update in gridview using procedures.

Code(.cs):-
public void update(object sender, GridViewUpdateEventArgs e)
        {
            SqlConnection conn = new SqlConnection(str_con);
            conn.Open();
            SqlCommand cmd1 = new SqlCommand("UpdateInfo", conn);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add("@s_name", SqlDbType.VarChar).Value = (gv1.Rows[e.RowIndex].Cells[1].Text);
            cmd1.Parameters.Add("@s_phone", SqlDbType.Int).Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells[2].Text);//<-----error shows here
            cmd1.Parameters.Add("@s_degree", SqlDbType.VarChar).Value = (gv1.Rows[e.RowIndex].Cells[3].Text);
            cmd1.Parameters.Add("@s_id", SqlDbType.Int).Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells[4].Text);
            cmd1.ExecuteNonQuery();
            gv1.EditIndex = -1;
            BindData();
            conn.Close();
        }


Stored procedures:-

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[UpdateInfo]
(
	@s_id int,
	@s_name varchar(300),
	@s_phone int,
	@s_degree varchar(300)
)
as
begin
update info set s_name=@s_name, s_phone=@s_phone, s_degree=@s_degree where s_id=@s_id
end
Posted
Comments
valerian.precop 6-Dec-11 6:46am    
What is the text inside gv1.Rows[e.RowIndex].Cells[2].Text?
What do you have there if you do a debug?
07navneet 6-Dec-11 7:18am    
I have done like this:
int.TryParse((gv1.Rows[e.RowIndex].Cells[2].Text), out phone);
and then
cmd1.Parameters.Add("@s_phone", SqlDbType.Int).Value = phone;
Now I am not getting error but table is not updated.
valerian.precop 6-Dec-11 7:47am    
If you do not get any errors that means you do not have the s_id you provided in your table.
What is the value for gv1.Rows[e.RowIndex].Cells[4].Text when you update?
07navneet 7-Dec-11 1:32am    
zero!
07navneet 7-Dec-11 1:43am    
Also, other values from cells are not showing their corr. values!

1.
If the value for your @s_id parameter meaning the value from gv1.Rows[e.RowIndex].Cells[4].Text is 0 than I do not think any update will be made because usually you do not have entries in the database with id = 0.

2.
If the incorrect values are on other cells too than the ones you edit, shows you have a problem with getting the results from database (the stored procedure/s that returns the rows for your datagrid binding).

3.
If the incorrect values are just for the editable row, than you have a problem when hitting the edit button (the edit event handler)

4.
Make sure you have the right index of the cell for all the values you want to get... Meaning, e.g. cell with index 4 is the right one for your @s_id parameter
 
Share this answer
 
Comments
07navneet 7-Dec-11 4:24am    
point #3 could be the posibl error! But how to handle/remove it.
07navneet 7-Dec-11 4:32am    
Is this correct for Edit button?
public void edit(object sender, GridViewEditEventArgs e)
{
gv1.EditIndex = e.NewEditIndex;
BindData();
}
07navneet 7-Dec-11 5:22am    
expecting answer from you sir!
Hi,
As you have used the phone number, better you will maintain the varchar type instead of Integer.

Hope this helps you.

Regards
Sheik
 
Share this answer
 
v2
Comments
07navneet 7-Dec-11 4:22am    
still having trouble!!
Using debugger, check the value in gv1.Rows[e.RowIndex].Cells[2].Text. Most likely it contains illegal characters and cannot be converted to integer.
 
Share this answer
 
Comments
07navneet 6-Dec-11 6:56am    
I am not able to check! But if it contains illegal characters then what is the solution?
Wendelius 6-Dec-11 7:45am    
You can use for example Int32.TryParse[^] to check the validity of the input before you actually use the value
07navneet 7-Dec-11 1:43am    
Also, other values from cells are not showing their corr. values!
Wendelius 7-Dec-11 13:49pm    
I think the best advice is that you try using debugger and see what are the values for each field/parameter/object. This helps you to identify the problem.
Hi navneeth,
i think you are giving non-numeric value as text,debug it and find value of it.
 
Share this answer
 
Comments
07navneet 6-Dec-11 7:12am    
I have done like this:
int.TryParse((gv1.Rows[e.RowIndex].Cells[2].Text), out phone);
and then
cmd1.Parameters.Add("@s_phone", SqlDbType.Int).Value = phone;

Now I am not getting error but table is not updated.
07navneet 7-Dec-11 1:33am    
I am getting zero!
07navneet 7-Dec-11 1:43am    
Also, other values from cells are not showing their corr. values! What to do??
Use TryParse method instead. This is safer than using Convert or Cast.

Regards,
Eduard
 
Share this answer
 
Comments
shek124 7-Dec-11 3:55am    
What would be difference when we use Tryparse?
07navneet 7-Dec-11 4:13am    
I have done like this:
int.TryParse((gv1.Rows[e.RowIndex].Cells[2].Text), out phone);
and then
cmd1.Parameters.Add("@s_phone", SqlDbType.Int).Value = phone;
Now I am not getting error but table is not updated. Also phone gets value=0!
use 'ItemTemplate' of gridview and 'findcontrol' of textboxes!
 
Share this answer
 

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