0% found this document useful (0 votes)
21 views

Practica de C # Insercion de Registros Datagrid

This document contains code for inserting records into a DataGrid control in a C# application. The code opens a SQL connection and loads data from a database table into a DataSet. It then adds a new row to the DataSet with values from text boxes, and updates the database. Finally, it sets the DataGrid's data source to display the updated records.

Uploaded by

MaikeJauregui
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Practica de C # Insercion de Registros Datagrid

This document contains code for inserting records into a DataGrid control in a C# application. The code opens a SQL connection and loads data from a database table into a DataSet. It then adds a new row to the DataSet with values from text boxes, and updates the database. Finally, it sets the DataGrid's data source to display the updated records.

Uploaded by

MaikeJauregui
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

I

PRACTICA DE C #
INSERCION DE REGISTROS DATAGRID
Disear el siguiente formulario.

private void InsertCommand_Click(object sender, System.EventArgs e)


{
string ConnectionString = "server=local;database= practica3;integrated security = SSPI";
SqlConnection conn = new SqlConnection(ConnectionString);
DataRow row;
DataSet ds = new DataSet();
try
{
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM ejemplo", conn);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
adapter.Fill(ds, "ejemplo");
row = ds.Tables["ejemplo"].NewRow();
row["idcliente"] = Convert.ToInt32(textBox1.Text);
row["apellidos"] = textBox2.Text;
row["nombre"] = textBox3.Text;
row["deuda"] = Convert.ToInt32(textBox4.Text);
ds.Tables["ejemplo"].Rows.Add(row);
adapter.Update(ds, "ejemplo");
dataGrid1.DataSource = ds.DefaultViewManager;
}
catch(SqlException exp)
{
MessageBox.Show(exp.Message.ToString());
}
if(conn.State == ConnectionState.Open)
conn.Close();
}

You might also like