Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.



C#
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);
            con.Open();
            int i = cmd.ExecuteNonQuery();

            con.Close();

            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.
Posted
Updated 19-Feb-18 8:21am
Comments
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.
[no name] 21-Aug-19 4:25am    
Kindly check your procedure once by passing the values to it in the SSMS.

1 solution

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.
SQL
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
SQL
sp_insert 'LASTNAME', 'COMPUTERNAME'
and see if any errors are shown.

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900