Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i got error in this coding
any one can help to correct this code



C#
private void Grid()
    {
        SqlConnection cn = new SqlConnection("Data Source=ANCYCHACKO-TOSH;Initial Catalog=tallysoftware;Integrated Security=True");

        SqlCommand cmd = new SqlCommand();
        DataTable dataTable;

        dataTable = new DataTable();

        cmd.Connection = cn;

        SqlDataAdapter da = new SqlDataAdapter("select * from Sheet1$ ", cn);

        System.Data.DataSet ds = new System.Data.DataSet();

        da.Fill(ds);

        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataSource = ds;
            GridView1.DataBind();
            cn.Close();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Grid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        Grid();
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Grid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["tallysoftwareConnectionString4"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cn.Open();
        GridViewRow row = GridView1.Rows[e.RowIndex];
        DropDownList dropdownlist1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("dropdownlist1");
        TextBox txtEcollected = (TextBox)row.FindControl("txtEcollected");
        TextBox txtEplan = (TextBox)row.FindControl("txtEplan");
        TextBox txtEacoll = (TextBox)row.FindControl("txtEacoll");
        DropDownList dropdownlist2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("dropdownlist2");
        TextBox txtEre = (TextBox)row.FindControl("txtEre");
        TextBox txtEdescrptn = (TextBox)row.FindControl("txtEdescrptn");
        //int Id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string se_name = dropdownlist1.SelectedItem.Text;
        string collected_date = txtEcollected.Text;
        string amount_plan = txtEplan.Text;
        string amount_collected = txtEacoll.Text;
        string status_ar = dropdownlist2.SelectedItem.Text;
        string remark = txtEre.Text;
        string description = txtEdescrptn.Text;
        Label lblid = (Label)row.FindControl("Label3");
        // string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        int id = Convert.ToInt32(lblid.Text);
        cmd.CommandText = "UPDATE Sheet1$ SET se_name='" + dropdownlist1.SelectedItem.Text + "' ,collected_date ='" + txtEcollected.Text + "',amount_plan ='" + txtEplan.Text + "',amount_collected ='" + txtEacoll.Text + "',status_ar ='" + dropdownlist2.SelectedItem.Text + "',remark='" + txtEre.Text + "',description='" + txtEdescrptn.Text + "' WHERE id=" + id + "";
        cmd.Connection = cn;
        cmd.ExecuteNonQuery();

        GridView1.EditIndex = -1;

        Grid();
        cn.Close();


    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["tallysoftwareConnectionString4"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = cn;
        cn.Open();
        GridViewRow row = GridView1.Rows[e.RowIndex];
        Label lblid = (Label)row.FindControl("Label3");
        // string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        int id = Convert.ToInt32(lblid.Text);
        //int Id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        cmd.CommandText = "DELETE FROM Sheet1$ WHERE id='" + id + "' ";

        cmd.ExecuteNonQuery();
        cn.Close();
        Grid();
    }
}
Posted
Updated 28-Nov-14 6:14am
v3
Comments
[no name] 28-Nov-14 8:27am    
Where are you getting error ,in which line ? and what error you getting???
Richard Deeming 28-Nov-14 14:32pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

try this:

C#
DropDownList dropdownlist1 = (DropDownList).row .FindControl("dropdownlist1");


or refer this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating%28v=vs.110%29.aspx[^]
 
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