Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace firstcrud
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if(con.State==ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();

            Disp();
        }
    protected void Button1_click(object sender, EventArgs e)
        {
                   SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into first values('" + ID.Text + "','" + firstName.Text + "','" + City.Text + "','" + Email.Text + "') ";
                cmd.ExecuteNonQuery();

            ID.Text = "";
                firstName.Text = "";
                City.Text = "";
                Email.Text = "";
            
            
                      Disp();
        }

        protected void Button2_click(object sender, EventArgs e)
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "delete from first where id ='"+ID.Text+"' ";
            cmd.ExecuteNonQuery();

            ID.Text = "";
            Disp();
        }


        protected void Button3_click(object sender, EventArgs e)
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update first set firstname = '"+firstName.Text+"',city='"+City.Text+"' , Email='"+Email.Text+"' where id =" + Convert.ToInt32(oldid.Text) + " ";
            cmd.ExecuteNonQuery();

            firstName.Text = "";
            City.Text = "";
            Email.Text = "";


            Disp();
        }


        protected void Button4_click(object sender, EventArgs e)
        {
            Disp();
        }
        public void Disp()
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from first";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }

    }

}


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="firstcrud.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>

                <tr>
                    <td>ID</td>
                    <td><asp:textbox ID="ID" runat="server"></asp:textbox></td>
                </tr>

                <tr>
                    <td>Name</td>
                    <td><asp:textbox ID="firstName" runat="server"></asp:textbox></td>
                </tr>

                
                <tr>
                    <td>City</td>
                    <td><asp:textbox ID="City" runat="server"></asp:textbox></td>
                </tr>

                
                <tr>
                    <td>Email</td>
                    <td><asp:textbox ID="Email" runat="server"></asp:textbox></td>
                </tr>

                <tr>
                    <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="Insert" />
                    <asp:Button ID="Button2" runat="server" Text="Delete" />
                    <asp:Button ID="Button3" runat="server" Text="Update" />
                    <asp:Button ID="Button4" runat="server" Text="Veiw" />
                    </td>
                </tr>

                <tr>
                    <td>Enter id for update</td>
                    <td><asp:textbox ID="oldid" runat="server"></asp:textbox></td>
                </tr>

            </table>

            <br />
            <asp:GridView ID="Gridview1" runat="server"></asp:GridView>
            
        </div>
    </form>
</body>
</html>


What I have tried:

i have tried debugging it many times but no errors is shown
Posted
Updated 27-Sep-21 18:57pm

So little code, so many mistakes...

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
And in a website? That's just suicidal...

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your app to start with, and replace the "one true connection" with a connection you create inside a using block when you need it, so the system Disposes of it when it goes out of scope at the same time.

Then replace the hard-coded connection with with one held in web.config so you can release your code without having to recompile it after testing ...

Do all that, and then see if you still have a problem...
 
Share this answer
 
Just because you don't get any errors, doesn't mean your code is correct or will work the way you think it will.

The debugger is there to debug YOU and your understanding of the code, not really the code itself. It's there to let you inspect the values of variables and other data structures any time you want so you can see what the values are and understand how they got that way. This allows adjusting your understanding of how things work instead of guessing at it like you are now.
 
Share this answer
 
Quote:
My program in ASP.NET dont show any errors but also dont give output at run time

Just because you don't get output, you know there is an error.
There is many levels of errors.
C# syntax errors are only the first level.
Then logical errors comes to play.
Then SQL errors comes too.
Quote:
i have tried debugging it many times but no errors is shown

I don't know what you mean by debugging, but debugging with or without debugger means to check at various points if anything is as expected.
Since you didn't gave us any detail, it is difficult to see what goes wrong.
Use debugger and/or add debugging code at various points to check which part is executed and to see internal variables, and run your code until you stop something wrong.
C#
cmd.CommandText = "insert into first values('" + ID.Text + "','" + firstName.Text + "','" + City.Text + "','" + Email.Text + "') ";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
thanks all of you for your solutions , i will try my best to write more parametized query from now on but the error was because i didn't define the onclick property of button ,but now i got that.
 
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