Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I Wanted to accept the value of Id field of the
Girdview which is as follows
<asp:BoundField DataField ="id" HeaderText="ID" Visible="false" />

How can assign this value to pass the parameter to sqlquery ?
Posted
Updated 25-Mar-14 22:40pm
v2

Try something like

If the ID field is the PrimaryKey , set it to the DataKeyNames property of the Gridview.

Then in the RowUpdating event you can try to access like

string ID= GridView1.DataKeys[e.RowIndex].Values["ID"].ToString();


SqlConnection con = new SqlConnection("YourConnectionString");

string query="UPDATE TableName set fieldName=@Parameter where Id=@ID";//parameter @ID,Can use StoredProcedure here instead of Inline Sql Query
con.Open();
SqlCommand Cmd = new SqlCommand(query, con);
Cmd.Parameters.AddWithValue("@ID", ID);
Cmd.ExecuteNonQuery();
GridView1.DataBind();
 
Share this answer
 
Comments
Ni!E$H_WAGH 24-Mar-14 3:00am    
Thanks JoCodes
JoCodes 24-Mar-14 3:02am    
You are Always Welcome.And thanks for Mark it as Answer.
Ni!E$H_WAGH 24-Mar-14 3:06am    
Dear JoCodes , I have written one method for udating the data using the MOdelPopupextender control

Am not sung RowUpdating so in this case How can get the value for the same ?
JoCodes 24-Mar-14 3:17am    
Can you share that code?
Ni!E$H_WAGH 24-Mar-14 3:25am    
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim temp As String = Session("emp_code")
Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
' Here I want access the value of ID but getting error e is not member of GridviewUpdateEventargs
Dim sum As Double = 0.0
Dim str1 As String = "select ISNULL(sum(MBO_Weighting_Factor),0) from tbl_MBO_Objective_Details where MBO_Req_Id is null and MBO_EMP_CODE='" & EMP_NO & "'" ' and id=@idnum"
Dim Comm1 As New SqlCommand
Comm1 = New SqlCommand(str1, Conn)
Comm1.Parameters.AddWithValue("@idnum", ID)

Conn.Open()
sum = Comm1.ExecuteScalar()
Conn.Close()
sum = sum + Convert.ToDouble(txtwtfactor.Text)
If sum > 1.0 Then
ScriptManager.RegisterStartupScript(Me.Page, Me.[GetType](), "Alert", "<script type='text/javascript'>testmsg3(' You can not update, Your weighting factors exceeds one ...!');</script>", False)
BindDataMBOPerObjectives()
BindDataMBOFuncObjDetails()
Else
Conn.Open()
Dim cmd As New SqlCommand("update tbl_MBO_Objective_Details set MBO_Objective=@funcobj,MBO_Measured_Var=@measurdvar,MBO_Target_Date=@targetdt,MBO_Weighting_Factor=@wtfact where id=@id", Conn) 'where UserId=@UserId
cmd.Parameters.AddWithValue("@funcobj", txtobjective.Text)
cmd.Parameters.AddWithValue("@measurdvar", txtmeasurdvar.Text)
cmd.Parameters.AddWithValue("@targetdt", txttargetdate.Text)
cmd.Parameters.AddWithValue("@wtfact", txtwtfactor.Text)
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(lblID.Text))
cmd.ExecuteNonQuery()

Conn.Close()

BindDataMBOFuncObjDetails()
BindDataMBOPerObjectives()
End If
End Sub
instead of bound filed use itemtemplate and bind this ID to link button and redirect to another page. this will be little bit easy for you.


<asp:linkbutton id="lnkEdit" runat="server" postbackurl="yourpagename.aspx?ID='<%#" xmlns:asp="#unknown">'
CausesValidation="false" Text="Edit">


i did not test this but it should work. let me know if you have any issues.
 
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