Click here to Skip to main content
15,877,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string connection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";
        SqlConnection cn = new SqlConnection(connection);
        cn.Open();

        string sqlquery = "INSERT INTO [users] (member, pass) VALUES (@memb ,@pas)";

        SqlCommand cmd = new SqlCommand(sqlquery, cn);

        cmd.Parameters.AddWithValue("@memb", SqlDbType.VarChar).Value = textBox1.Text;
        cmd.Parameters.AddWithValue("@pas", SqlDbType.VarChar).Value = textBox2.Text;

        int i = (int)cmd.ExecuteNonQuery();

        if (i > 0)
        {
            this.Close();
            Form2 form2 = new Form2();
            form2.Show();
            MessageBox.Show("Data updated", "Adding data", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        else
        {
            MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        cn.Close();


Can someone explain it to me, why it doesn't add data to database ?
Posted
Updated 24-Jan-12 3:48am
v2
Comments
Abdul Quader Mamun 24-Jan-12 9:02am    
Where is the explanation???

hi,

This post described somewhat similar problem: http://stackoverflow.com/questions/3317868/insert-problem-in-c-using-sqlcommand[^]

I would also suggest to move cn.Close(); to below
C#
int i = (int)cmd.ExecuteNonQuery();
because the cn.Close() method never get call if i > 0
 
Share this answer
 
v2
your Database means Database1.mdf required some configurations.

1. The Database1.mdf should be placed within Bin\dubug folder of your application.

2. Select Database1.mdf, open properties windows by pressing F4. the "Copy to Output Directory " should be set as "Do not copy".
it may generate error so first time change it to "Copy if newer" then change it to "Do not copy".



There is no Problem with the code written by you.
Hope this will help you.
Happy Coding.
 
Share this answer
 
Comments
Ryszard50 24-Jan-12 17:52pm    
Hi Sourav92, thanx for a hint but when I changed to "do not copy" I always get this message : An attempt to attach an auto-named database for file C:\Documents and Settings\.....\bin\Release\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. unless I change it to "copy if never" after that there is no error but it doesnt write data to database.
[no name] 25-Jan-12 10:46am    
now see there will be a folder in your Bin\Debug
named as bin\debug. delete the folders first.
You are using local database. You just try with the new object of SqlParameter. Otherwise it is ok...
 
Share this answer
 
Comments
Ryszard50 25-Jan-12 4:28am    
Can you give an example 'cos I can't see your point. Sorry but Im beginer in programming.
May be this is the reason insertion is failing,you are casting the result of ExcecuteNonQuery()

int i = (int)cmd.ExecuteNonQuery(); 


ExecuteNonQuery() returns integer so no need to cast it

Or the other reason may be your primary key is not autoincrement and should be given in insert statement
 
Share this answer
 
Comments
CodeHawkz 24-Jan-12 11:35am    
Should not be the case :) Converting int to an int or converting any type to the same type should not fail in any case. I mean, there is NO logical explanation for that :)

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