there i have created a simple program from storing login username and password.. in sql database..
table name: [/b]login
fieds: 1) urname 2) pass
there are two buttons NEW, and SAVE..
and two TextBoxs named: username, password
form load event;
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Server=server;Database=test;uid=sa;pwd=goldstar");
dslogin = new DataSet();
dalogin = new SqlDataAdapter("select urname, pass from login", conn);
SqlCommandBuilder cmdbld = new SqlCommandBuilder(dalogin);
conn.Open();
dalogin.Fill(dslogin, "login");
dataGridView1.DataSource = dslogin;
dataGridView1.DataMember = "login";
currmgr = (CurrencyManager)this.BindingContext[dslogin.Tables["login"]];
textBox1.DataBindings.Add("Text", dslogin.Tables["login"], "urname");
textBox2.DataBindings.Add("Text", dslogin.Tables["login"], "pass");
}
new button code:
textBox1.Clear(); textBox2.Clear();
SAVE Button code:
dalogin.InsertCommand = conn.CreateCommand();
dalogin.InsertCommand.CommandText = "insert into login " + "(urname,pass) " + "values " + "(?,?)";
dalogin.InsertCommand.Parameters.Add("urname", SqlDbType.NVarChar, 50, "urname");
dalogin.InsertCommand.Parameters.Add("pass", SqlDbType.NVarChar, 50, "pass");
DataTable datatable = dslogin.Tables[0];
DataRow rw = datatable.NewRow();
rw["urname"] = textBox1.Text.Trim();
rw["pass"] = textBox2.Text.Trim();
try
{
dslogin.Tables["login"].Rows.Add(rw);
dalogin.Update(dslogin, "login");
dslogin.AcceptChanges();
Application.DoEvents();
}
catch (Exception x)
{
}
WHEN i press the save button my changes are shown to the gridview but when i close the application and restart it the changes are gone.. they are not there.
and also my changes are not made to my sql database..
This post has been edited by sandeepparekh9: 09 February 2009 - 11:30 PM

New Topic/Question
Reply




MultiQuote


|