Cannot add data to SQL Server Database from my ASP website form

426 Views Asked by At

I'm pretty new to SQL Databases & C# language, I can't add data to my SQL Database from my web application despite doing everything correctly? I'm currently training in Azure SQL & C# & have been using the following training video:

https://www.youtube.com/watch?v=POWm4EfU9bA

I'm using MS Visual Studio and MS SQL Server Management Studio, I'm also using Azure Web + SQL service and I have opened up the firewall allowing my client IP address through.

Can anyone work out, from my code, why I my data from my webform isn't being added to my database? Here is my coding which "apparently" adds data to my SQL database from the form located in my front end web application:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace bluecitywebapplication
{
    public partial class WebForm1 : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == true)
            {
                Label1.Text = ("Great job!");

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection stormconn = new SqlConnection("Server=tcp:bluecitydb.database.windows.net,1433;Initial Catalog=xxx;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
            {
                SqlCommand insert = new SqlCommand("EXEC dbo.InsertFullname @Fullname", stormconn);
                insert.CommandType = System.Data.CommandType.StoredProcedure;
                insert.Parameters.AddWithValue("@Fullname", TextBox1.Text);
                insert.Parameters.AddWithValue("@Address", TextBox2.Text);
                insert.Parameters.AddWithValue("@Email", TextBox3.Text);


                stormconn.Open();
                insert.ExecuteNonQuery();
                stormconn.Close();

                if (IsPostBack)
                {
                    TextBox1.Text = ("");
                }
            }
        }
    }
}
1

There are 1 best solutions below

9
Ali Hassan On

sorry, I apologize for the mistake, I thought that you are facing connection issue in your question. I read the code carefuly i found there is issue in the code of executing procedure. I updated the code.

 protected void Button1_Click(object sender, EventArgs e)
 {
    using (var connection = new MySqlConnection("Server=tcp:xxxx.database.windows.net,1433;Initial Catalog=xxxx;Persist Security Info=False;User ID=xxxxx;Password=xxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"))
    {
        MySqlCommand command = new MySqlCommand("InsertFullname", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new MySqlParameter("Fullname", TextBox1.Text));

        command.Connection.Open();
        var result = command.ExecuteNonQuery();
        command.Connection.Close();
    } 
 }