Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning how to use database in c# for that i have created an application that checks or adds user
Problem is that
1.when ever solution is running the values are added to database and are shown but as soon as i recompile the solution then all the previous data is gone
2.what ever data i store in database it gets stored with spaces at its end for eg user name entered in text box is Afnan it gets stored and displayed with spaces as Afnan........ where . represent space.
I added MDF file and created table in that.


using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Data.SqlClient;<br />
using System.Data.Sql;<br />
namespace Neo_Soft_Project_Manegment<br />
{<br />
    public partial class Form1 : Form<br />
    {<br />
        public Form1()<br />
        {<br />
            InitializeComponent();<br />
        }<br />
<br />
     <br />
        private void Form1_Load(object sender, EventArgs e)<br />
        {<br />
<br />
           // MessageBox.Show(usersTableAdapter1.);<br />
<br />
        }<br />
<br />
        private void button_login_Click(object sender, EventArgs e)<br />
        {<br />
     <br />
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";<br />
            SqlConnection cn = new SqlConnection(connectionString);<br />
            try<br />
            {<br />
                cn.Open();<br />
              <br />
            }<br />
            catch<br />
            {<br />
                MessageBox.Show("Unable to connect");<br />
            }<br />
<br />
            string Uname = text_user.Text;<br />
            string Pass = text_pass.Text;<br />
            string sqlquery = ("SELECT*FROM Users WHERE user_name = '" + text_user.Text + "'");<br />
            sqlquery = " INSERT INTO [Users](user_name,user_pass) VALUES ('"+text_user.Text+"','"+text_pass.Text+"')";<br />
            SqlCommand command = new SqlCommand(sqlquery, cn);<br />
            command.ExecuteNonQuery();<br />
            <br />
            if (command != null)<br />
                command.Dispose();<br />
            if (cn != null)<br />
                cn.Close();            <br />
        }<br />
<br />
        private void button1_Click(object sender, EventArgs e)<br />
        {<br />
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";<br />
            SqlConnection cn = new SqlConnection(connectionString);<br />
            try<br />
            {<br />
                cn.Open();<br />
            }<br />
            catch<br />
            {<br />
                MessageBox.Show("Unable to connect");<br />
            }<br />
            SqlCommand cmd = new SqlCommand("SELECT * FROM Users", cn);<br />
            cmd.Connection = cn;<br />
            SqlDataReader reader = null;<br />
            reader = cmd.ExecuteReader();<br />
            while (reader.Read())<br />
            {<br />
                MessageBox.Show(reader["user_name"].ToString() + reader["user_pass"].ToString());<br />
                if (text_user.Text == (reader["user_name"].ToString()) && text_pass.Text == (reader["user_pass"].ToString()))<br />
                {<br />
                    MessageBox.Show("Loged In");<br />
                }<br />
                else MessageBox.Show("Failed to Login");<br />
            }<br />
        }<br />
<br />
   }<br />
}<br />
Posted

If you are using SQL Express Server with user instance:

1. during build is copied the database from sources to the build target folder.
2. executed application uses the copied database (such as one from bin\debug)
3. after rebuild/rerun is your database replaced by the original one stored in sources (probably entirely empty one).


Spaces at the end of value may be caused by choosen datatype (such as char or nchar). Try varchar or nvarchar insted if it's the case.
 
Share this answer
 
v2
Comments
ifithegr8 14-Jul-10 11:43am    
Reason for my vote of 5
Answer was logical and helped me removing my bugs
Make sure your MDF file is not set to be copied to your output directory.

In Solution Explorer, check to see what the build action is and make sure it's not overwriting your database.

Cheers.
 
Share this answer
 
Mr James if I change MDF file settings to do not copy then database connection fails
 
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