Click here to Skip to main content
15,173,823 members
Home / Discussions / C#
   

C#

 
QuestionLoad pID and productname to combobox - c sharp. Pin
misCafe27-May-09 16:04
MembermisCafe27-May-09 16:04 
AnswerRe: Load pID and productname to combobox - c sharp. Pin
Satish Pai27-May-09 20:20
MemberSatish Pai27-May-09 20:20 
GeneralRe: Load pID and productname to combobox - c sharp. Pin
ScottM127-May-09 21:47
MemberScottM127-May-09 21:47 
GeneralRe: Load pID and productname to combobox - c sharp. Pin
misCafe27-May-09 23:47
MembermisCafe27-May-09 23:47 
GeneralRe: Load pID and productname to combobox - c sharp. Pin
ScottM127-May-09 23:54
MemberScottM127-May-09 23:54 
QuestionActiveX component problems Pin
rasikrodri27-May-09 14:43
Memberrasikrodri27-May-09 14:43 
QuestionTesting Localized Form (Debug vs non-Debug) Pin
cpl27-May-09 14:43
Membercpl27-May-09 14:43 
QuestionUpdate records problem Pin
desir10227-May-09 14:15
Memberdesir10227-May-09 14:15 
Hello
I am trying to update record in a database. I can see the changes in the datagrid, but they don't get saved into the database.. The 3 textboxes show the values of the currently selected row in the datagrid. So i press the Change button to make the textboxes active, change the text and then click the Save button.
Here's how the form looks like: http://img529.imageshack.us/my.php?image=45903634.jpg


using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private SqlConnection conn;
        private string connStr;
        SqlDataAdapter da;
        DataSet ds;
        bool b;

        public Form1()
        {
            InitializeComponent();
            connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\dbOne.mdf" + ";Integrated Security=True;Connect Timeout=30;User Instance=True";
            conn = new SqlConnection(connStr);
            conn.Open();
            string selectStr = "select * from people";
            da = new SqlDataAdapter(selectStr, conn);
            ds=new DataSet();
            da.Fill(ds,"people");

            da.InsertCommand = new SqlCommand("insert into people(name, age) values (@name, @age)", conn);
            da.InsertCommand.Parameters.Add("@name",SqlDbType.VarChar,50,"name");
            da.InsertCommand.Parameters.Add("@age",SqlDbType.Int,5,"age");

            da.UpdateCommand = new SqlCommand("update people set name=@name, age=@age where id=@id",conn);
            da.UpdateCommand.Parameters.Add("@name",SqlDbType.VarChar,50,"name");
            da.UpdateCommand.Parameters.Add("@age",SqlDbType.Int,5,"age");
            da.UpdateCommand.Parameters.Add("@id",SqlDbType.Int,5,"id");

            da.DeleteCommand = new SqlCommand("delete from people where id=@id",conn);
            da.DeleteCommand.Parameters.Add("@id",SqlDbType.Int,5,"id");

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.DataSource = ds.Tables["people"];
            txtId.DataBindings.Add("Text",ds.Tables["people"],"id");
            txtName.DataBindings.Add("Text", ds.Tables["people"], "name");
            txtAge.DataBindings.Add("Text", ds.Tables["people"], "age");

            txtId.Enabled = false; txtName.Enabled = false; txtAge.Enabled = false; 
            btnSave.Enabled = false; btnCancel.Enabled = false;
            conn.Close();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            b = false;
            txtName.Enabled = true; txtAge.Enabled = true;
            btnNew.Enabled = false; btnDelete.Enabled = false; btnSave.Enabled = true; btnCancel.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {            
            if (b) // insert new line
            {
                DataRow newLine = ds.Tables["people"].NewRow();
                newLine["name"] = txtName.Text;
                newLine["age"] = txtAge.Text;
                ds.Tables["people"].Rows.Add(newLine);
            }
            da.Update(ds,"people");
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            b = true;
            int i = ds.Tables["people"].Rows.Count;
            dataGridView1.Rows[i].Selected = true;

            txtId.Text = ""; txtName.Text = ""; txtAge.Text = "";
            txtName.Enabled = true; txtAge.Enabled = true;
            btnSave.Enabled = true; btnCancel.Enabled = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int i = int.Parse(dataGridView1.CurrentRow.Index.ToString());
            ds.Tables["people"].Rows[i].Delete();
            da.Update(ds,"people");
        }


    }
}

AnswerRe: Update records problem Pin
Vimalsoft(Pty) Ltd27-May-09 21:50
professionalVimalsoft(Pty) Ltd27-May-09 21:50 
QuestionWhy multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 14:08
MemberAljaz11127-May-09 14:08 
AnswerRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 14:40
sitebuilderLuc Pattyn27-May-09 14:40 
GeneralRe: Why multiselected text files don't open in more tabs [modified] Pin
Aljaz11127-May-09 14:51
MemberAljaz11127-May-09 14:51 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 15:26
sitebuilderLuc Pattyn27-May-09 15:26 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 15:29
MemberAljaz11127-May-09 15:29 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 15:38
sitebuilderLuc Pattyn27-May-09 15:38 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 15:55
MemberAljaz11127-May-09 15:55 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 15:58
sitebuilderLuc Pattyn27-May-09 15:58 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 16:25
MemberAljaz11127-May-09 16:25 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 16:33
sitebuilderLuc Pattyn27-May-09 16:33 
QuestionC++ to C# (struct) Pin
Mohammad Dayyan27-May-09 13:57
MemberMohammad Dayyan27-May-09 13:57 
AnswerRe: C++ to C# (struct) [not an answer] Pin
Colin Angus Mackay27-May-09 14:27
MemberColin Angus Mackay27-May-09 14:27 
GeneralRe: C++ to C# (struct) [not an answer] Pin
Mohammad Dayyan27-May-09 15:00
MemberMohammad Dayyan27-May-09 15:00 
GeneralRe: C++ to C# (struct) [not an answer] Pin
Colin Angus Mackay27-May-09 21:50
MemberColin Angus Mackay27-May-09 21:50 
AnswerRe: C++ to C# (struct) Pin
Adam Maras27-May-09 15:53
MemberAdam Maras27-May-09 15:53 
AnswerRe: C++ to C# (struct) Pin
Jimmanuel27-May-09 15:54
MemberJimmanuel27-May-09 15:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.