This is my first foray into the world of programming. I'm doing a practice project for the consulting firm I work for, and my current objective is to make my submission page textboxes insert the user input into specific tables in my database. Later I'll need to retrieve the data, but for now it suffices to get the data to go where it's supposed to. Why won't the data go into it's home? Doesn't it like it's home?

My code is below. I'm working with VS2010, code behind in C#, with SQLEXPRESS 2005. When I debug, punch in sample input and submit, I find no changes in the table data. Ideas?

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

public partial class SubmissionPage : System.Web.UI.Page
{
  
    //public SqlConnection sqlConn = new SqlConnection("'data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\\ArgonautDatabase;User Instance=true' providerName='System.Data.SqlClient'");

   // public SqlConnection sqlConn = new SqlConnection(".\\SQLEXPRESS;AttachDbFilename='|DataDirectory|\\ArgonautDatabase.mdf';Integrated Security=True;User Instance=True;");
     
    //public SqlConnection sqlConn = new SqlConnection(".\\SQLEXPRESS;AttachDbFilename='C:\\Users\\Brazos\\Documents\\Visual Studio 2010\\Projects\\Argonaut Insurance Site\\Argonaut Insurance Site\\App_Data\\ArgonautDatabase.mdf';Integrated Security=True;User Instance=True;");
     
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
       // sqlConn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\aspnetdb.mdf;Integrated Security=True;User Instance=True";
       sqlConn.ConnectionString = " Data Source = .\\SQLEXPRESS; Initial Catalog = Submission; User Id = sa; Password = sa";

        sqlConn.Open();
        SqlCommand sqlComm = new SqlCommand();
        sqlComm = sqlConn.CreateCommand();
        sqlComm.CommandText = @"INSERT INTO Customer (" + " Name, SIC_NAIC, Address, City, State, Zip, " + ") VALUES (" + " Textbox1.Text, RadioButtonList1.SelecedItem, TextBox2.Text, DropDownList1.SelectedItem, TextBox3.Text" +
            ")";
        sqlComm.CommandText = @"INSERT INTO Broker (" + " Name, Broker_Id, Address, City, State, Zip, Entity_Type" + ") VALUES (" + " TextBox5.Text, TextBox18.Text, TextBox6.Text, TextBox7.Text, DropDownList2.Text, TextBox8.Text, DropDownList3.Text" +
            ")";
sqlComm.ExecuteNonQuery();
sqlConn.Close();

    }
}

Dani AI

Generated

This thread is a C# WebForms (code‑behind) issue, so is correct that it fits better in the .NET forums. The original post from exhibits several classic beginner mistakes that explain why rows never appear: the SQL string is being built incorrectly (control names end up inside the SQL), there are syntax problems (extra commas / mismatched parentheses), the CommandText for the first INSERT is overwritten by the second, and no parameters are used (SQL injection and quoting errors). There’s also a small typo in the radio button reference (Selected spelled wrong), and the connection/DB being inspected can differ from the one the app touches (Visual Studio file-copy or wrong connection string).

Recommended checklist (fast troubleshooting)

  • Run the code under the debugger and watch for exceptions; add a try/catch that logs exceptions.
  • Inspect the actual connection string and confirm the same database is being viewed in SQL Server Management Studio.
  • Log or inspect the final CommandText sent to SQL Server (or use SQL Profiler) to see the real query.
  • Fix typos such as SelectedItem/SelectedValue, and ensure lists provide the expected value/text.

Minimal safe pattern (use parameterized queries and using blocks)

using (var conn = new SqlConnection(connString))
{
    conn.Open();
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "INSERT INTO MyTable (Name, State, Zip) VALUES (@name, @state, @zip)";
        cmd.Parameters.Add("@name", SqlDbType.NVarChar, 200).Value = txtName.Text;
        cmd.Parameters.Add("@state", SqlDbType.NVarChar, 50).Value = ddlState.SelectedValue;
        cmd.Parameters.Add("@zip", SqlDbType.NVarChar, 20).Value = txtZip.Text;
        cmd.ExecuteNonQuery();
    }
}

For two inserts either execute two separate commands (or clear/replace parameters and call ExecuteNonQuery again), or wrap both in a transaction so both succeed or both roll back.

Final notes: move the connection string to web.config, avoid using the sa account with a trivial password, validate and sanitize inputs server‑side, and prefer explicitly typed parameters instead of AddWithValue for predictable SQL types.

Please try posting in the .NET forums. This section is for Classic ASP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.