Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code

error occure in this line
C#
string query = "UPDATE [WebCrm].[dbo].[tbl_Lead] SET FirstName='" + textLead.Text + "',Date='" + textDate.Text + "',Status='" + textStatus.Text + "',PotentialRevenue='" + textRevenue.Text + "',ProductInterest='" + textProduct.Text + "',Owner='" + textOwner.Text + "',Company='" + textCompany.Text + "' where LedId='" + Convert.ToInt32(lblLedId.Text) + "'";



C#
protected void grdLead_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           SqlConnection con = new SqlConnection(cn.ConnectionStrings);
           Label lblLedId = (Label)grdLead.FindControl("lblLedId");
           TextBox textLead = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textLead");
           TextBox textCompany = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textCompany");
           TextBox textProduct = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textProduct");
           TextBox textRevenue = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textRevenue");
           TextBox textDate = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textDate");
           TextBox textStatus = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textStatus");
           TextBox textOwner = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textOwner");
           string query = "UPDATE [WebCrm].[dbo].[tbl_Lead] SET FirstName='" + textLead.Text + "',Date='" + textDate.Text + "',Status='" + textStatus.Text + "',PotentialRevenue='" + textRevenue.Text + "',ProductInterest='" + textProduct.Text + "',Owner='" + textOwner.Text + "',Company='" + textCompany.Text + "' where LedId='" + Convert.ToInt32(lblLedId.Text) + "'";
           con.Open();
           SqlCommand cmd = new SqlCommand(query,con);
           cmd.CommandType = CommandType.Text;
           cmd.ExecuteNonQuery();
           con.Close();
           grdLead.EditIndex = -1;
           bindLead();


UPDATE: Code from repost:

XML
<asp:GridView ID="grdLead" DataKeyNames="LedId" runat="server" AllowPaging="True" AllowSorting="True" CssClass="table table-bordered" AutoGenerateColumns="False" EnableModelValidation="True" CellPadding="0" GridLines="None" Width="100%" ShowFooter="True" OnPageIndexChanging="grdLead_PageIndexChanging" OnRowCancelingEdit="grdLead_RowCancelingEdit" OnRowDeleting="grdLead_RowDeleting" OnRowEditing="grdLead_RowEditing" OnRowUpdating="grdLead_RowUpdating">
                              <Columns>
                                  <asp:BoundField DataField="LedId" HeaderText="Sr.No"  />
                                  <asp:BoundField DataField="FirstName" HeaderText="Lead Name"/>
                                  <asp:BoundField DataField="Date" HeaderText="Date"  />
                                  <asp:BoundField DataField="Status" HeaderText="Status"  />
                                  <asp:BoundField DataField="PotentialRevenue" HeaderText="PotentailRevenuer" />
                                  <asp:BoundField DataField="ProductInterest" HeaderText="ProductInterest"  />
                                  <asp:BoundField DataField="Owner" HeaderText="Owner" />
                                  <asp:BoundField DataField="Company" HeaderText="CompanyName"  />
                                  <asp:CommandField ShowEditButton="True"  />
                                  <asp:CommandField ShowDeleteButton="True"/>
                              </Columns>
                          </asp:GridView>
Posted
Updated 15-Oct-15 23:42pm
v3
Comments
Andy Lanng 16-Oct-15 5:42am    
You don't need to repost. It really rubs people the wrong way.

You can use the "Improve Question" button above this comment to add detail. I'll add the ASP for you

1 solution

First off: You should never build queries with parameter injection. This leaves you wide open to SQL injection attacks which can destroy your data. Use parameterized queries instead. This will also help you find the problem:

C#
//No need to generate the query string every time.
private const string query1 = @"
UPDATE [WebCrm].[dbo].[tbl_Lead] 
SET FirstName=@firstname, 
    Date=@date, 
    Status=@status,
    PotentialRevenue=@revenue,
    ProductInterest=@interest,
    Owner=@owner,
    Company=@company 
where LedId=@ledId";

protected void grdLead_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           SqlConnection con = new SqlConnection(cn.ConnectionStrings);
           Label lblLedId = (Label)grdLead.FindControl("lblLedId");
           TextBox textLead = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textLead");
           TextBox textCompany = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textCompany");
           TextBox textProduct = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textProduct");
           TextBox textRevenue = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textRevenue");
           TextBox textDate = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textDate");
           TextBox textStatus = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textStatus");
           TextBox textOwner = (TextBox)grdLead.Rows[e.RowIndex].FindControl("textOwner");
           string query = query1; 

            SqlCommand cmd = new SqlCommand(query, con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@firstname", SqlDbType.NVarChar) { Value = textLead.Text });
            //You can specify different datatypes if you need to:
            cmd.Parameters.Add(new SqlParameter("@date", SqlDbType.Date) { Value = textDate.Text });
            cmd.Parameters.Add(new SqlParameter("@status", SqlDbType.NVarChar) { Value = textStatus.Text });
            cmd.Parameters.Add(new SqlParameter("@revenue", SqlDbType.NVarChar) { Value = textRevenue.Text });
            cmd.Parameters.Add(new SqlParameter("@interest", SqlDbType.NVarChar) { Value = textProduct.Text });
            cmd.Parameters.Add(new SqlParameter("@owner", SqlDbType.NVarChar) { Value = textOwner.Text });
            cmd.Parameters.Add(new SqlParameter("@company", SqlDbType.NVarChar) { Value = textCompany.Text });
            cmd.Parameters.Add(new SqlParameter("@ledId", SqlDbType.Int) { Value = lblLedId.Text });

           
           con.Open(); //only open the connection when you're ready to execute
           int rowsChanged = cmd.ExecuteNonQuery();
           con.Close();
           //if(rowsChanged==1)
             //success
           //else
             //failure
           grdLead.EditIndex = -1;
           bindLead();


Use that and you will see exactly where the error is.

PS: one of your controls was not found ^_^

UPDATE: You can simplify the method as so:
C#
protected void grdLead_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           SqlConnection con = new SqlConnection(cn.ConnectionStrings);
           Label lblLedId = (Label)grdLead.FindControl("lblLedId");
           TextBox textLead = (TextBox)e.Row.FindControl("textLead");
           TextBox textCompany = (TextBox)e.Row..FindControl("textCompany");
           TextBox textProduct = (TextBox)e.Row..FindControl("textProduct");
           //Etc...


I'll keep looking into the root issue
 
Share this answer
 
v5
Comments
Radhika20 16-Oct-15 5:15am    
using this code error will occure "cmd variable allready used"
Andy Lanng 16-Oct-15 5:26am    
Ah yes. There was a copy-paste error

I have updated the solution

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