Click here to Skip to main content
13,831,589 members
Rate this:
Please Sign up or sign in to vote.
See more:
The Problem I have is that my data is not being submitted into the database or the datatable at all when I click on the save button.

What supposed to happen is that the user (or me) for this instance is supposed to enter the information in two textboxes then click on the save button to enter the information in the database. However it doesn't save in the database or does show in the gridview even though it says and looks like the data is being saved.

There are no errors in my code. It's a simple sign in form that i'm creating so i'm not to worried about sql injection at this moment. although I will add code to prevent sql injection once i'm done with the project. I will continue to debug and look for solutions. If anyone can answer or address this issue it would be greatly appreciated.

My code is below and please note I did not copy and paste this code from the internet I wrote it myself. Again thank you for your time if anyone can help me.

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ComputerLab.mdf;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("sp_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@LASTNAME", metroTextBox1.Text);
            cmd.Parameters.AddWithValue("@COMPUTER", metroTextBox2.Text);
            int i = cmd.ExecuteNonQuery();


            if (i != 0)
                MessageBox.Show(i + "Data Saved");

What I have tried:

I have debugged the program. Tested the connection string. nothing I have thought of so far has seemed to help the situation any.
Updated 19-Feb-18 9:21am
PIEBALDconsult 18-Feb-18 20:41pm
We'd need to see definition of the table and sp_insert . You do know not to begin a procedure name with "sp_", don't you?
ZurdoDev 18-Feb-18 21:28pm
If your code executes each line with no error then the problem is in the sql.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First thing to check is that you are not overwriting the database everytime you start your program - is the datadirectory a sub-folder of your project? Are you querying the release version or the debug version if it is?

Try running the following code in Sql Server Management Studio after connecting to that database using the connection details from your code.
insert into ComputerLab (LASTNAME,COMPUTER) values('LASTNAME','COMPUTER')
If any errors are reported then address those errors - you may have a column on that table that cannot be NULL but which you are not inserting for example.

Try running the stored procedure directly from SSMS
and see if any errors are shown.

Do you have multiple schemas within your database? Try
SELECT * FROM dbo.ComputerLab

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190114.1 | Last Updated 19 Feb 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100