0

getting error while initializing command text .unable add sql update query in sql command object

TextBox id = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtid");
TextBox Loginid = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtgvusername");
TextBox EmployeeId = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtEmployeeId");
TextBox Fullname = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtFullname");
TextBox Password = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtPassword");
TextBox ContactNo = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtContactNo");
TextBox MailId = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtMailid");
TextBox Location = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtLocation");
TextBox Roles = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtRoles");

CheckBox chkLeads = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("chkLeads");
CheckBox ChkSales = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("ChkSales");
CheckBox ChkReports = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("ChkReports");
CheckBox ChkPayments = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("ChkPayments");

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update tbl_AdminLogin set FullName='" + txtfullName.Text + "',Password='" + txtPassword.Text + "',ContactNo='" + txtContact.Text + "',Mailid='" + txtMailid.Text + "',Location='" + txtLocation.Text + "',Roles='" + Roles.Text + "',Leads='" + chkLeads.Text + "',Sales='" + ChkSales.Checked + "',Reports='" + ChkSales.Text + "',Payments='" + ChkPayments.Text + "',EmployeeId='" + id.Text + "' where id='" + id.Text + "'";//getting error here
d4Rk
  • 6,622
  • 5
  • 46
  • 60
krishna mohan
  • 905
  • 4
  • 20
  • 38
  • 5
    **[Possible SQL Injection](https://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx)** – Lukasz Szozda Feb 13 '16 at 11:50
  • 2
    Storing password as plain text is not good idea either. – Lukasz Szozda Feb 13 '16 at 11:51
  • `id` is null, you are not getting any textbox in `id` – Hemal Feb 13 '16 at 11:51
  • no ..id is not null. im getting id value is "18" – krishna mohan Feb 13 '16 at 11:54
  • A wild guess due to lack of more information: NullReferenceException as one of the textboxes is null? – Nicholas Schuster Feb 13 '16 at 12:15
  • as a test, you could set a string variable to your update string and display it somewhere so you can see how it appears to the command object when it's submitted. This should show you what's missing. Also, Password is usually a reserved keyword in most database systems. You should consider wrapping that to denote it as a field and not a keyword or more preferably call it something other than Password. – Charles May Feb 13 '16 at 12:22

1 Answers1

2

One of the Controls you're trying to find couldn't be found and FindControl returned null, and you were using the Text (or Checked) property of that null control. I don't know what are the names of your controls but I suspect that the problem is in that line:

    CheckBox chkLeads = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("chkLeads");

Because you the names of the rest of your CheckBoxes got prefix "Chk" instead of "chk", try to fix it, and take a look on the first two comments on your question.

Aly Elhaddad
  • 1,913
  • 1
  • 15
  • 31