last day (16 days later) » 
06:00 - 13:0014:00 - 16:00

6:42 AM
0
A: Get selected Row in a pop up for Edit and Update

Mairaj AhmadHere is a solution using ajax. When you check a checkbox and click on update button jquery code will look for selected checkbox in gridview and when it finds this will assign values to textboxes which will be in popup. Also you have to store id in hidden field on the basis of which update query ...

 
Yes only one row per check,can I start implementing that ?
 
First check it and understand it if there is any confusion clear it after that start implementing.
 
Where should I write the <div id="Popup"> in aspx page only ?
 
You will use this as popup so you can place it anywhere on the page.
 
ok. I am still confused with the WebMethod logic, where should I define that ?
 
6:42 AM
You will define it in the page on which you want to update the gridview i.e in same page where you have gridview.
 
Ok, I am starting implementing that, will let u know if any doubt comes
 
ok start and if any question i m here
 
ok, sure
started with the html part
 
6:45 AM
ok
 
I also added a checkbox in the gridview
like this
<asp:TemplateField HeaderText="Select" HeaderStyle-Width="5%" HeaderStyle-CssClass="k-grid td">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
is it fine ?
 
yes.But you do not need to change code of gridview
 
7:03 AM
ok
done with html part
what is
url: "Default.aspx/UpdateDB", ?
Default.aspx here ?
the name of my current page ?
 
yes
updatedb is name of webmethod
 
ok
i am getting error as
ASP.adduserprofile_aspx' does not contain a definition for 'OpenSelected' and no extension method 'OpenSelected' accepting a first argument of type 'ASP.adduserprofile_aspx' could be found (are you missing a using directive or an assembly reference?)
 
?
Ok this is javascript function not code behind define this in js file
 
ok
shd I add one new js file
?
 
no if you have already one add this function in that file
 
7:12 AM
ok
how to change the. eq here ?
function OpenSelected() {
$('table[id$=grdUser] tr').each(function () {

if ($(this).find('input[type=checkbox]').is(':checked')) {
$('#txtuser').val($(this).children('td').eq('02').text());
$('#txtpass').val($(this).children('td').eq('02').text());
$('#txtemail').val($(this).children('td').eq('02').text());
$('#ddlusertype').val($(this).children('td').eq('02').text());
$('#ddlactive').val($(this).children('td').eq('02').text());
$('#hdnID').val($(this).children('td').eq('01').text());
}


});
}
also shd I remove the onClick on the button click ?
 
eq means the number of td in row
 
So how should I add here the eq ?
 
just call this functtion onlick of button you want
 
ok
I have eq like this
function OpenSelected() {
$('table[id$=grdUser] tr').each(function () {
if ($(this).find('input[type=checkbox]').is(':checked')) {
$('#txtuser').val($(this).children('td').eq('02').text());
$('#txtpass').val($(this).children('td').eq('03').text());
$('#txtemail').val($(this).children('td').eq('04').text());
$('#ddlusertype').val($(this).children('td').eq('05').text());
$('#ddlactive').val($(this).children('td').eq('06').text());
$('#hdnID').val($(this).children('td').eq('01').text());
}
});
}
is it fine ?
 
if txtuser will get value from td no 1 write eq(01)
 
7:19 AM
ohhk
so what shd I write for hdnId ?
as it is after the td
 
how will you update the DB. I mean on basis of which column ?
 
on the basis of Id
 
and have you selected this column in gridview ?
 
yes
eariler for my update
the query was like this
UPDATE tbl_User SET username=@username, email=@email, usertype=@usertype, active=@active WHERE Id=@Id
 
and how will you get this id ?
 
7:26 AM
like this
string Id = grdUser.DataKeys[e.RowIndex].Value.ToString();
 
No now you are using ajax so you can't do this simply add another boundfeild in gridview and bind it id
 
ok
but I dont want to display the Id in the gridview
i will set its visible = false
 
and select this in query where you are populating gridview
yes make it visible false
 
ok
so the OpenSelected()
code of mine's is right
?
i still haven't write the query
 
yes its fine
which query ?
 
7:37 AM
the UpdateDB
can you help in making the query ?
 
this is simply the update query which you just shared with me
 
so
it is the same query
?
that shd i write
 
yes query will remain same only you have to add webmethod in .cs file
which i posted
 
ok
when I am adding
[WebMethod]
i get error as
the type or namespace webmethod could not be found
 
add this in your namespces
using System.Web.Services;
and this will be like WebMethod
 
7:42 AM
ok
i added like this
public static string UpdateDB(string Id, string username, string password, string email, string usertype, string ddlactive)
{

}
is it ok ?
 
yes but on top of this you have to write [WebMethod]
 
yes written
 
ok its fine than
 
ok
I added
somewhat like this
string strQueryForUpdate = "Update tbl_User SET username=@username, email=@email, usertype=@usertype, ddlactive=@ddlactive Where Id=@Id";
return "User is updated";
is it fine
?
 
yes but have you written code for executing this query :)
 
7:56 AM
no
i added this much only
pls help in writing the query
 
write code for executing query now i am leaving for lunch will be back in hour
ok here is some code
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
ok
i am also going
for lunch
 
8:14 AM
let me know once u come
 
8:41 AM
i am back
 
me 2
u there ?
 
8:56 AM
yes
 
ok
what to write in query ?
for updating ?
 
UPDATE tbl_User SET username=@username, email=@email, usertype=@usertype, active=@active WHERE Id=@Id
 
So, this is my UpdateDB function
public static string UpdateDB(int Id, string user, string email, string usertype, string ddlactive)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
string query = "UPDATE tbl_User SET username=@username, email=@email, usertype=@usertype, active=@active WHERE Id=@Id";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return "User is updated";
}
 
i missed one thing in code i gave to you add parameters to cmd like you added when creating user
cmd.Parameters.AddWithValue("@username",user);
and same for all other parameters
yes rest is fine
 
ok
let me add that
added like below
public static string UpdateDB(int Id, string user, string email, string usertype, string ddlactive)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
string query = "UPDATE tbl_User SET username=@username, email=@email, usertype=@usertype, active=@active WHERE Id=@Id";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@username", user);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@usertype", usertype);
is it fine ?
 
9:11 AM
also add for @id
 
ok
i am getting error at
<asp:Button ID="btnupdate1" runat="server" CssClass="btn btn-prm" Text="Update" OnClick="OpenSelected()" />
'ASP.adduserprofile_aspx' does not contain a definition for 'OpenSelected' and no extension method 'OpenSelected' accepting a first argument of type 'ASP.adduserprofile_aspx' could be found (are you missing a using directive or an assembly reference?)
 
change this
<asp:Button ID="btnupdate1" runat="server" CssClass="btn btn-prm" Text="Update" OnClick="OpenSelected()" />
<input type="button" value="Update" onclick="Openselected()" />
to
 
to what ?
 
<input type="button" value="Update" onclick="Openselected()" />
 
ok
i checked the page
the form is visible
it should be on the click of update button
 
9:28 AM
yes
 
so
on page load the form is coming
 
???????
 
when I open the page
the form is coming
it should be visible on update button
See the image
 
yes for now leave it and complete the flow as i told you
we will add popup for this later once everything is ready
 
so what's the next step
?
i am getting the form as blank
atleast the form should be with the data of which I want to open in pop up and update it
 
9:41 AM
when you check a checkbox and click update button what happens ?
 
as already said
the form is opening on pageload
let me check with that
when I check the checkbox and click the button
the gridview row moves
the form doesn't opens
as it is already opened on the page_load
 
now when you click the update button which is in popup div do you hit the breakpoint in webmethod ?
 
i did not debugged, let me add a breakpoint
no it is not hitting the breakpoint
 
have you added the function UpdateGrid in Js ?
 
i have written that in aspx page
 
9:50 AM
ok
 
so what is the reason?
 
put alert in it and check that function is executing ?
 
show me UpdateGrid function
 
no alert is not working
<script type="text/javascript">
function UpdateGrid() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "AddUserProfile.aspx/UpdateDB",
data: JSON.stringify({ category: $('#txtuser').val(), items: $('#txtpass').val(), items: $('#txtemail').val(), items: $('#ddlusertype').val(), items: $('#ddlactive').val(), id: $('#hdnID').val() }),
dataType: "json",
async: false,
success: function (data) {
$('#txtuser').val("");
$('#txtpass').val("");
$('#txtemail').val("");
$('#ddlusertype').val("");
 
9:53 AM
ok show me the button on which you are calling it ?
 
also you have one mistake in this in data: change items to variable name which is passed to webmethod like you have passed user in webmethod so write user in data part
data: JSON.stringify({ user: $('#txtuser').val(), items: $('#txtpass').val(), email: $('#txtemail').val(), usertype: $('#ddlusertype').val(), items: $('#ddlactive').val(), id: $('#hdnID').val() }),
correct all parameters in this
 
i didn't got u
for ex
('#txtuser') should be changed to $('user')
like this ?
 
then
 
10:01 AM
where you written items it should be name of parameter defiend in webmethod like i changed items to user ,
just see my example once more and you will be clear
 
ohh k
got it
what about
hdnId
?
data: JSON.stringify({ user: $('#txtuser').val(), email: $('#txtemail').val(), usertype: $('#ddlusertype').val(), ddlactive: $('#ddlactive').val(), Id: $('#hdnID').val() }),
i changed like above
 
yes this is right
hdnid is id of row on basis of which update query will run
 
should I move ahead ?
ohk
 
10:05 AM
show me button on which you are calling UpdateGrid method
 
i am getting alert message as : Invalid web service call
ok
<input type="button" runat="server" value="submit" onclick="UpdateGrid()" />
 
ok you are missing value of some parameter
 
as in
i have added all the parameters
 
compare the number of parameters in WebMethod and in data
 
10:07 AM
and also case should be same i.e if you have written user in data it should be user in WebMethod also
 
yes it is same
and I checked all the parameters also
are all matching
 
ok now tell me which browser are you using ?
 
chrome
do u want to check my code
?
 
just a minute
 
10:11 AM
Press F12 in chrome and console will be shown there click on network here you will see your ajax call and error tell me that error
 
ok
POST localhost:49924/AddUserProfile.aspx/UpdateDB 500 (Internal Server Error)
also
Uncaught ReferenceError: jQuery is not defined jquery.sortable.js:107
Uncaught ReferenceError: $ is not defined
 
click on Post localhost.........
it will give you details
 
(anonymous function) jquery.min.js:2
j jquery.min.js:2
k.fireWith jquery.min.js:2
n.extend.ready jquery.min.js:2
K
this error
sorry
that was not the error
this is the error
send jquery.min.js:4
n.extend.ajax jquery.min.js:4
UpdateGrid my_js.js:34
onclick
 
no click on UpdateDB/AddUserProfile.aspx and see error
 
where ?
 
10:17 AM
this is not related to ajax error which you have givn
 
i didn't got u
 
in broswer
console click network
and than click UpdateDB/AddUserProfile.aspx
 
and send me Webmethod code
 
i cant see
UpdateDB/AddUserProfile.aspx in Netwoek
network*
Webmethod code
public static string UpdateDB(int Id, string user, string email, string usertype, string ddlactive)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
string query = "UPDATE tbl_User SET username=@username, email=@email, usertype=@usertype, active=@active WHERE Id=@Id";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@username", user);
cmd.Parameters.AddWithValue("@email", email);
 
10:22 AM
Oh man you have given my connection name is connectionstring change it to your connection string
 
Shitt man
how can I do this
 
check name of your connection string in web.config
 
i added that
still the same error
 
now debug it
 
10:26 AM
Have you changed name of connection string in thisl line
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Defau‌​ltConnection"].ToString());
Write name of your connection string in place of DefaultConnection
 
yes
i changed it
i added the breakpoint at WebMethod
but it is not hitting it
onClick of update button
I am getting alert
Invalid webservice call, missing value for parameters
 
yes that's what i told you earlier
 
yes
 
what is full text in alert it should also be giving name of parameter which is missng
 
wait i will give u
see
Message: "Invalid web service call, missing value for parameter: 'Id'."
what i got on network
click
u there ?
 
10:44 AM
yes
 
did u saw the error
 
yes i see that
 
you have not assigned the value to hdnvalue
 
means
 
10:50 AM
have you created the hidden feild and assigned value to it just like u did to other textboxes of popup
post code of Openselected() function
 
yes
<asp:HiddenField ID="htnId" runat="server" />
See the whole html
<div id="Popup">
<table style="width: 100%" class="tbl">
<tr>
<td style="vertical-align: top;">
<asp:Label CssClass="control-label" ID="Label1" runat="server">Username</asp:Label>
</td>
<td>
<div class="control-group">
<div class="controls">
<asp:TextBox ID="txtuser" runat="server" placeholder="Enter a Username" class="form-control"></asp:TextBox>
</div>
</div>
</td>
</tr>

<tr>
<td style="vertical-align: top;">
<asp:Label CssClass="control-label" ID="Label4" runat="server">EmailId</asp:Label>
</td>
 
ok now paste your javascript function OpenSelected
You made id bound feild visible false this will cause error
make it hidden by css display:none
assign a property to that bound feild like this
 
function OpenSelected() {
$('table[id$=grdUser] tr').each(function () {
if ($(this).find('input[type=checkbox]').is(':checked')) {
$('#txtuser').val($(this).children('td').eq('01').text());
$('#txtemail').val($(this).children('td').eq('02').text());
$('#ddlusertype').val($(this).children('td').eq('03').text());
$('#ddlactive').val($(this).children('td').eq('04').text());
$('#htnId').val($(this).children('td').eq('01').text());
}
});
}
ok
 
<asp:BoundField DataField="id" HeaderText="Page Title" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" ControlStyle-CssClass="hidden" />
and in css class hidden make it display:none
i have added this to bound feild ControlStyle-CssClass="hidden"
hidden is name of css class and in this i have written display:none
 
ok
let me add in css file too
done
is the OpenSelected() function correct ?
 
11:01 AM
yes looks good
now refresh page and check
 
and also put break point in webmethod
 
i can see the ID
in the gridview
 
press ctrl+f5 in browser to get latest changes
what have you written in hidden class ?
 
.hidden {
display: none;
}
 
11:04 AM
ok are you still seeing this
 
yes
 
where is this class defined and have you assigned this bound feild ?
 
the class is defined in style.css
i have defined this bound field inside the gridview
 
ok have you referenced this file on your page ?
 
yes
 
11:06 AM
paste code of id bound feild only
 
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" ControlStyle-CssClass="hidden" />
 
ok we will see that now just to check update any row and check if there is some error or not
 
ok
so i will now check the checkbox and click on update button ?
 
ok
it is still the same error
{"Message":"Invalid web service call, missing value for parameter: \u0027Id\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 parameters)\r\n at
even the breakpoint is also not hittinh
hittinh*
I dont know where I am going wrong
 
11:14 AM
change asp:hidden feild to
<input type="hidden" id="hdnID" />
and id will remain same what you have at your end
 
when you leave your office ?
 
I have time
i am here till 8 o clock
 
and what is time now there ?
 
here it is now 4.43
pm
still the same thing
same error
 
11:26 AM
ok i wil connect with you via remote at 7 is it ok
 
ok
fine
by the time
i am checing why it is not working
 
Put alert in UpdateDb and check values are proper like this
alert($('#htnId').val())
 
alert($('#txtuser').val())
and like this all other values
 
it is not in
UpdateDb
it is in
UpdateGrid
 
11:35 AM
sorry i forgot this ):
 
hehe
ok np
 
check and tell me if you are getting something wrong
 
ok
alert is also not working
I am dumb on this now
going for a tea break
will come back and see
 
11:50 AM
ok
When you come back add this in id bound feild to hide it
HeaderStyle-CssClass="hidden" ItemStyle-CssClass="hidden"
 
yes i m back
ok
i can still see the ID column
but its value is hidden now
 
value is hidden ?
 
means
nothing is displayed in the row
only I can see the HeaderText
 
12:05 PM
remove header text property
i have same code here and it is hidden
 
12:30 PM
u there ?
 
12:44 PM
ok
wait
i m in meeting
 
ok
going home will contact u from there
bye
 
ok
bye
khuda hafiz
tc
let me know once you come from their
 
06:00 - 13:0014:00 - 16:00

  last day (16 days later) »