0

I want to insert data using ssms but not inserted data in ssms using .net core??

Insertion code, WebForm1.aspx.cs:

namespace InsertUpdateDelete.scripts
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void btninsert_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            string address = txtAddress.Text;
            Insert_Click(name, address);
        }

        void Insert_Click(string name, string address)
        {
            SqlConnection con = new SqlConnection(connstring);

            string query = "Insert into DemoInUpDelete (Name, Address) values (@Name,@Address)";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.CommandType = CommandType.Text;
            //Pass values to Parameters
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Address", address);
            try
            {
                con.Open();
                int validateOperation = cmd.ExecuteNonQuery();
                if (validateOperation > 0)
                {
                    //Message insert succesfully
                }
                else
                {
                    //Error
                }
            }
            catch (SqlException e)
            {
                //Exception
            }
            finally
            {
                con.Close();
            }

        void Update_Click(object sender, EventArgs e)
        {
               //update
        }

        void Delete_Click(object sender, EventArgs e)
        {
                   //delete
        }


    }
}



WebForm.aspx:


<body>


    <form id="form1" runat="server">

    <table>
         <tr>  
            <td>Name:</td>  
            <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>  
        </tr>  

        <tr>  
            <td>Address:</td>  
            <td><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>  
            <td> </td>  
        </tr>  

        <tr>  
            <td>  
               <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />
               <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="Update_Click" />  
               <asp:Button ID="btnCancel" runat="server" Text="Delete" OnClick="Delete_Click" />
            </td>  
        </tr>  
  </table>

    </form>
</body>

I m new in.net how to insert data in sql server SQL Server Management Studio? I m creating a simple new->project but not insert data?? DataBase: I m creating database and Id(primarykey) name(varchar(20) address(varchar(20)

.net give error

SqlConnection con = new SqlConnection(connstring); connstring does not exist the current context

web.config.cs:

<configuration>

  <connectionStrings>
    <add name="ConnStringName" connectionString="Data Source= DESKTOP-U3PB1TF\SA; Integrated Security=true;Initial Catalog= InUpDelete; uid=sa; Password=admin@123; " providerName="System.Data.SqlClient" />
  </connectionStrings>

</connectionStrings>

Lorenzo Isidori
  • 1,809
  • 2
  • 20
  • 31
Rahul Patil
  • 142
  • 14
  • 3
    You're missing the SqlCommand!! The SqlConnection is commented out, basically you need to follow a guide on SQL inserts from C#. Please follow a proper tutorial because your SQL is injectable. I'll try and find a duplicate. – Jeremy Thompson Sep 19 '19 at 08:36
  • 2
    Besides missing SqlCommand, **never ever** concatenate user input to run a command, use query parameters instead (see also: [general information on SQL Injection](https://www.owasp.org/index.php/SQL_Injection)) – Sergey Kudriavtsev Sep 19 '19 at 08:39
  • 1
    This question on second thought doesn't make sense. ASP.NET Core does not support WebForms and the Microsoft Team has said they have no plans to port webforms to asp.net core. – Jeremy Thompson Sep 19 '19 at 08:43
  • Possible duplicate of [How to insert data into SQL Server](https://stackoverflow.com/questions/12241084/how-to-insert-data-into-sql-server) – Jeremy Thompson Sep 19 '19 at 08:51

1 Answers1

3

@Jeremy Thompson Stated as comment you miss the Insert operation code

Below code may help you.

aspx Code:

 <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />  

C# code:

 protected void btninsert_Click(object sender,EventArgs e)
 {
   string name=txtName.Text;
   string address=txtAddtess.Text;
   Insert_Click(name,address);
 }


 void Insert_Click(string name,string address)
    {
        string connstring=System.Configuration.ConfigurationManager.
ConnectionStrings["ConnStringName"].ConnectionString;

        SqlConnection con = new SqlConnection(connstring);

        string query = "Insert into DemoInUpDelete (Name, Address) values (@Name,@Address)";           
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType=CommandType.Text;            
        //Pass values to Parameters
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Price", address);         
        try
        {
            con.Open();
            int validateOperation=cmd.ExecuteNonQuery();
    if(validateOperation>0)
    {
        //Message insert succesfully
            }
            else
            {
             //Error
             }               
        }
        catch (SqlException e)
        {
           //Exception
        }
        finally
        {
            con.Close();               
        }
}
Divyesh patel
  • 967
  • 1
  • 6
  • 21
  • Asp.net says No overload for 'Insert_Click' matches delegate 'EventHandler – Rahul Patil Sep 19 '19 at 09:06
  • You have a method like Insert_Click(object sender,EventArgs e){} or btninsert_Click(object sender,EventAgs e) ? – Divyesh patel Sep 19 '19 at 09:10
  • Thanks for yor code it is very helpful for beginner it is easily to implement Asp.net give error An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Login failed for user 'sa'. i think connection string error – Rahul Patil Sep 19 '19 at 09:17
  • Show edit answer and replace password in connection string from **** to your actual password. – Divyesh patel Sep 19 '19 at 09:21
  • perhaps its not a best practice, you have to define your connection string in web.config file and then after access it. – Divyesh patel Sep 19 '19 at 09:22
  • @ FrustratedDeveloper can u see my edited question. data not inserted?how to access a connection string(web.config) in my current page webform1.aspx.cs – Rahul Patil Sep 19 '19 at 09:45
  • @ FrustratedDeveloper thanks give your valuable time thanks man your great sugggestion.can u give any referance for learn .net and asp.net?? – Rahul Patil Sep 19 '19 at 09:56