Click here to Skip to main content
15,888,401 members
Home / Discussions / C#
   

C#

 
QuestionLoad pID and productname to combobox - c sharp. Pin
misCafe27-May-09 15:04
misCafe27-May-09 15:04 
AnswerRe: Load pID and productname to combobox - c sharp. Pin
Satish Pai27-May-09 19:20
Satish Pai27-May-09 19:20 
GeneralRe: Load pID and productname to combobox - c sharp. Pin
ScottM127-May-09 20:47
ScottM127-May-09 20:47 
GeneralRe: Load pID and productname to combobox - c sharp. Pin
misCafe27-May-09 22:47
misCafe27-May-09 22:47 
GeneralRe: Load pID and productname to combobox - c sharp. Pin
ScottM127-May-09 22:54
ScottM127-May-09 22:54 
QuestionActiveX component problems Pin
rasikrodri27-May-09 13:43
rasikrodri27-May-09 13:43 
QuestionTesting Localized Form (Debug vs non-Debug) Pin
cpl27-May-09 13:43
cpl27-May-09 13:43 
QuestionUpdate records problem Pin
desir10227-May-09 13:15
desir10227-May-09 13: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 20:50
professionalVimalsoft(Pty) Ltd27-May-09 20:50 
QuestionWhy multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 13:08
Aljaz11127-May-09 13:08 
AnswerRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 13:40
sitebuilderLuc Pattyn27-May-09 13:40 
GeneralRe: Why multiselected text files don't open in more tabs [modified] Pin
Aljaz11127-May-09 13:51
Aljaz11127-May-09 13:51 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 14:26
sitebuilderLuc Pattyn27-May-09 14:26 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 14:29
Aljaz11127-May-09 14:29 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 14:38
sitebuilderLuc Pattyn27-May-09 14:38 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 14:55
Aljaz11127-May-09 14:55 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 14:58
sitebuilderLuc Pattyn27-May-09 14:58 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Aljaz11127-May-09 15:25
Aljaz11127-May-09 15:25 
GeneralRe: Why multiselected text files don't open in more tabs Pin
Luc Pattyn27-May-09 15:33
sitebuilderLuc Pattyn27-May-09 15:33 
QuestionC++ to C# (struct) Pin
Mohammad Dayyan27-May-09 12:57
Mohammad Dayyan27-May-09 12:57 
AnswerRe: C++ to C# (struct) [not an answer] Pin
Colin Angus Mackay27-May-09 13:27
Colin Angus Mackay27-May-09 13:27 
GeneralRe: C++ to C# (struct) [not an answer] Pin
Mohammad Dayyan27-May-09 14:00
Mohammad Dayyan27-May-09 14:00 
GeneralRe: C++ to C# (struct) [not an answer] Pin
Colin Angus Mackay27-May-09 20:50
Colin Angus Mackay27-May-09 20:50 
AnswerRe: C++ to C# (struct) Pin
Adam Maras27-May-09 14:53
Adam Maras27-May-09 14:53 
AnswerRe: C++ to C# (struct) Pin
Jimmanuel27-May-09 14:54
Jimmanuel27-May-09 14: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.