GridView Update?

Discussion in 'ASP .Net Datagrid Control' started by sebastian.buschdorf, Dec 12, 2006.

  1. 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
    sebastian.buschdorf, Dec 12, 2006
    #1
    1. Advertising

  2. I've got it! :)


    "sebastian.buschdorf" <> schrieb
    im Newsbeitrag news:elltlh$q92$...
    > 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
    >
    sebastian.buschdorf, Dec 12, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hartmut Schroth
    Replies:
    3
    Views:
    3,498
    Hartmut Schroth
    Dec 1, 2003
  2. joun
    Replies:
    9
    Views:
    2,234
    W.G. Ryan eMVP
    Nov 30, 2004
  3. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,956
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  4. mesut
    Replies:
    0
    Views:
    660
    mesut
    Feb 19, 2007
  5. Evan M.
    Replies:
    1
    Views:
    1,197
    Evan M.
    Apr 18, 2007
Loading...

Share This Page