1

I am creating a register like program using C# and a local SQL Server database.

I have managed to create a query that inputs the data into the table called UserAdd.

Code:

ALTER PROCEDURE UserAdd 
    @Name VARCHAR(30), 
    @Organisation VARCHAR(20),
    @DOB DATETIME,
    @Contact VARCHAR(50),
    @Address VARCHAR(250) 
AS 
    INSERT INTO Member(Name, Organisation, DOB, Contact, Address)
    VALUES (@Name, @Organisation, @DOB, @Contact, @Address)

After creating this I used the code below to ensure it was passed through the query and added to the database:

private void Btn_Submit_Click(object sender, EventArgs e)
{
        if (Txt_NameReg.Text == "")
        {
            MessageBox.Show("Please fill required fields(*)");
        }
        else
        {
            using (SqlConnection SQLCon = new SqlConnection(ConnectionString))
            {
                SQLCon.Open();

                SqlCommand SqlCmd = new SqlCommand("UserAdd", SQLCon);
                SqlCmd.CommandType = CommandType.StoredProcedure;

                SqlCmd.Parameters.AddWithValue("@Name", Txt_NameReg.Text.Trim());
                SqlCmd.Parameters.AddWithValue("@Organisation", Txt_OrgReg.Text.Trim());
                SqlCmd.Parameters.AddWithValue("@DOB", DT_DobReg.Value.ToString("dd-MM-yyyy"));
                SqlCmd.Parameters.AddWithValue("@Contact", Txt_RegCont.Text.Trim());
                SqlCmd.Parameters.AddWithValue("@Address",Txt_RegAddress.Text.Trim());

                SqlCmd.ExecuteNonQuery();

                MessageBox.Show("Registration Successfull");
                Clear();
            }
        }
    }

    void Clear()
    {
        Txt_NameReg.Text = Txt_OrgReg.Text = DT_DobReg.Text = Txt_RegCont.Text = Txt_RegAddress.Text = "";
    }

During the testing of this I receive the expected message of "Registration successful"

However when I look at the database values, they are empty and I receive this error:

enter image description here

How can I ensure the data is input into the table?

EDIT: Connection string:

string ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\GardenRegister.mdf;Integrated Security=True";
halfer
  • 19,824
  • 17
  • 99
  • 186
WMTS
  • 53
  • 1
  • 1
  • 12
  • Your error is "no process is on the other end of the pipe". Do any of [these answers](https://stackoverflow.com/questions/27267658/no-process-is-on-the-other-end-of-the-pipe-sql-server-2012) help? – Adam V Jan 04 '18 at 15:24
  • Thanks @AdamV , but they do not help in relation to me, they all use the server assistant or older versions, I am currently using Visual studio with the inbuilt T-SQL addons to connect to a local database, any ideas on fixing it in an up to date version? – WMTS Jan 04 '18 at 15:32

1 Answers1

0

Try Closing your connection with SQLCon.Close(); at the end of your using statement.

Also move SQLCon.Open(); to the line before you execute the query (Although that is nothing to do with your issue but best practice)

SE1986
  • 2,534
  • 1
  • 10
  • 29
  • Thank you for this information, however the data is still not input to the database and a new error appeared detailing: "This database cannot be imported, it is either an unsupported SQL Server version or an unsupported database compatability" – WMTS Jan 04 '18 at 16:41
  • Can you post your connection string? – SE1986 Jan 04 '18 at 16:56
  • Yes, i'll add it to the original post above – WMTS Jan 04 '18 at 17:45