GridView Update?

  • Thread starter sebastian.buschdorf
  • Start date
S

sebastian.buschdorf

Hello,

I'm new in programming with ASP.NET 2.0 and I have a problem with the
updating of the Grid View!
I have one GridView with a connection to a database, three textboxes and a
button! The Values of the textboxes should wrote
down to the database, after clicking the button! That's worked, but the
GridView don't show the Values after that! When I click
the button the second time, then the GridView shows the Values, but he had
then two records in the database of that value!

Here is my Code:

protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString =
"server=buero3;Trusted_Connection=yes;database=Personal";
SqlConnection conn = new SqlConnection(ConnectionString);

conn.Open();

string Sql = "SELECT * FROM Mitarbeiter";

SqlCommand cmd = new SqlCommand(Sql, conn);
SqlDataReader reader = cmd.ExecuteReader();

myGridView.DataSource = reader;

DataBind();

myGridView.GridLines = GridLines.Both;

conn.Close();

lblDatum.Text = DateTime.Now.ToString();

txtName.Focus();

}
protected void btnAnmelden_Click(object sender, EventArgs e)
{
string ConnectionString =
"server=buero3;Trusted_Connection=yes;database=Personal";
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Mitarbeiter (Name, Vorname,
Abteilung) VALUES (@valName, @valVorname, @valAbteilung)";
cmd.Parameters.Add("@valName", SqlDbType.VarChar, 50).Value =
txtName.Text;
cmd.Parameters.Add("@valVorname", SqlDbType.VarChar, 50).Value =
txtVorname.Text;
cmd.Parameters.Add("@valAbteilung", SqlDbType.VarChar, 50).Value =
txtAbteilung.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();


Regards,
Sebastian

P.S. Sry for my bad English! *g
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top