Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
designed page code of gridview in asp.net
ASP.NET
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False" DataKeyNames="SID" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                        <Columns>
                            <asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False" ReadOnly="True" SortExpression="SID" />
                            <asp:BoundField DataField="StudentName" HeaderText="StudentName" SortExpression="StudentName" />
                            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
                            <asp:BoundField DataField="ParentName" HeaderText="ParentName" SortExpression="ParentName" />
                            <asp:BoundField DataField="MobileNo" HeaderText="MobileNo" SortExpression="MobileNo" />
                            <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
                            <asp:BoundField DataField="PickUpPoint" HeaderText="PickUpPoint" SortExpression="PickUpPoint" />
                            <asp:BoundField DataField="BusStatus" HeaderText="BusStatus" SortExpression="BusStatus" />
                            <asp:BoundField DataField="BusFees" HeaderText="BusFees" SortExpression="BusFees" />
                            <asp:BoundField DataField="TutionFees" HeaderText="TutionFees" SortExpression="TutionFees" />
                            <asp:BoundField DataField="StationaryFees" HeaderText="StationaryFees" SortExpression="StationaryFees" />
                            <asp:BoundField DataField="OtherFee" HeaderText="OtherFee" SortExpression="OtherFee" />
                            <asp:BoundField DataField="TotalFees" HeaderText="TotalFees" SortExpression="TotalFees" />
                            <asp:BoundField DataField="PaidFees" HeaderText="PaidFees" SortExpression="PaidFees" />
                            <asp:BoundField DataField="Remaining" HeaderText="Remaining" SortExpression="Remaining" />
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                        </Columns>
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

c# code:
C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
     int i = e.RowIndex;
     DataKey dk = GridView1.DataKeys[i];
     con.Open();
     OleDbCommand cmd = new OleDbCommand("delete from StudentDetails where SID='" + Convert.ToInt16(dk.Value) + "'", con);

     cmd.ExecuteNonQuery();
     con.Close();
     bind();

 }

error occurring on cmd.executenonquery:
An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code

Additional information: Data type mismatch in criteria expression
Posted

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. If you don't, you'll leave your code vulnerable to SQL Injection[^].

In this particular case, since you have converted the parameter to a number, it's unlikely that anyone could exploit it. However, it's a good idea to get into the habit of always using parameters, so that you don't miss a potentially-exploitable vulnerability.

Also, it looks like you're storing the connection in a field. That's not a good idea. You should create the connection as late as possible, and dispose of it as soon as you're finished using it.
C#
private static OleDbConnection CreateConnection()
{
    return new OleDbConnection("....");
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int i = e.RowIndex;
    DataKey dk = GridView1.DataKeys[i];
    
    using (OleDbConnection con = CreateConnection())
    using (OleDbCommand cmd = new OleDbCommand("delete from StudentDetails where SID = ?", con))
    {
        // OleDb doesn't use named parameters, so only the order matters here:
        cmd.Parameters.AddWithValue("SID", dk.Value);
        
        con.Open();
        cmd.ExecuteNonQuery();
    }
    
    bind(); 
}
 
Share this answer
 
Comments
Arasappan 26-Nov-15 5:34am    
like
Most probably SID column is integer in StudentDetails table. But in query SID is used as text data. So it is throwing data type mismatch. Code should like below:
C#
OleDbCommand cmd = new OleDbCommand("delete from StudentDetails where SID=" + Convert.ToInt16(dk.Value) + "", con);
 
Share this answer
 
v2
Check the datatype of column SID in table StudentDetails. It may be Integer type. if it is integer type then no need to give it as string type.
 
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