Neleron
Neleron

Reputation: 13

Invalid input - format string ASP.NET

Hello I'm quite new in ASP.NET programming

Dropdownlist code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection test = new SqlConnection("Data Source=MICHAŁ-KOMPUTER;Initial Catalog=ProjektNet_s10772;Integrated Security=True");

    SqlCommand com;
    string str;
    test.Open();
    str = "Select * from Klient Where Klient_ID='" + DropDownList1.Text.Trim() + "'";
    com = new SqlCommand(str, test);
    SqlDataReader reader = com.ExecuteReader();
    if (reader.Read())
    {
        txtimie2.Text = reader["Imie"].ToString();
        txtnazwisko2.Text = reader["Nazwisko"].ToString();
        txtemail2.Text = reader["Adres_Email"].ToString();
        txttelefon2.Text = reader["Telefon"].ToString();
        txtmiasto2.Text = reader["Miasto"].ToString();
        reader.Close();
        test.Close();
    }
}

Update Code

 protected void btnedytuj1_Click(object sender, EventArgs e)
{
    SqlConnection edytuj = new SqlConnection("Data Source=MICHAŁ-KOMPUTER;Initial Catalog=ProjektNet_s10772;Integrated Security=True");
    {

        SqlCommand cmd = new SqlCommand("Update Klient SET Imie='"+txtimie2.Text+"', Nazwisko='"+txtNazwisko+"', Adres_Email='"+txtemail2+"', Telefon='"+txttelefon2+"', Miasto='"+txtmiasto2+"' WHERE Klient_ID='"+Convert.ToInt32 (txtmiasto2.Text).ToString()+"'", edytuj);

        edytuj.Open();
        cmd.ExecuteNonQuery();
        edytuj.Close();
        GridView3.DataBind();
    }
}

What I'm Trying to do: I'm trying To Update my GrindView form ( By using the TxtBoxes which are located outside of the Grindview form ) Dropdownlist list is working as I expected (Fullfiling the textboxes with date from Grindview ) - however Update Buttton isn't. When I'm running this Updated code form I'm getting

Invalid input - format string

Row 141: SqlCommand cmd = new SqlCommand("Update Klient SET Imie='"
+txtimie2.Text+"', Nazwisko='"+txtNazwisko+"', Adres_Email='"+txtemail2+"', Telefon='"
+txttelefon2+"', Miasto='"+txtmiasto2
+"' WHERE Klient_ID='"+Convert.ToInt32 (txtmiasto2.Text).ToString()+"'", edytuj);

Could someone try to help me? Thank

Upvotes: 0

Views: 305

Answers (3)

Neleron
Neleron

Reputation: 13

Ok Guys, problem Fixed, Thank for answers:

Code:

SqlConnection edytuj = new SqlConnection("Data Source=MICHAŁ-KOMPUTER;Initial Catalog=ProjektNet_s10772;Integrated Security=True");
    {

        SqlCommand xp = new SqlCommand("Update Klient SET Imie = @imie, Nazwisko = @Nazwisko, Adres_Email = @Adres_Email, Telefon = @Telefon, Miasto= @Miasto Where Klient_ID = @Klient_ID", edytuj);

        xp.Parameters.AddWithValue("@Imie", txtimie2.Text);
        xp.Parameters.AddWithValue("@Nazwisko", txtnazwisko2.Text);
        xp.Parameters.AddWithValue("@Adres_Email", txtemail2.Text);
        xp.Parameters.AddWithValue("@Telefon", txttelefon2.Text);
        xp.Parameters.AddWithValue("@Miasto", txtmiasto2.Text);
        xp.Parameters.AddWithValue("@Klient_ID", DropDownList1.Text.Trim());
        edytuj.Open();
        xp.ExecuteNonQuery();
        edytuj.Close();


        GridView3.DataBind();

    }

Upvotes: -1

Nazir Ullah
Nazir Ullah

Reputation: 610

oh don't use single quotation with integer type value

e.g.

WHERE Klient_ID="+Convert.ToInt32 (txtmiasto2.Text).ToString()+""

Upvotes: 0

fsacer
fsacer

Reputation: 1402

You should work on this code:

using (SqlConnection edytuj = new SqlConnection("Data Source=MICHAŁ-KOMPUTER;Initial Catalog=ProjektNet_s10772;Integrated Security=True"))
using (SqlCommand cmd = new SqlCommand("Update Klient SET Imie=@imie, Nazwisko=@nazwisko, Adres_Email=@email, Telefon=@telefon, Miasto=@miasto WHERE Klient_ID=@id", edytuj))
{

    cmd.Parameters.AddWithValue("@imie", txtimie2.Text);
    cmd.Parameters.AddWithValue("@nazwisko", txtNazwisko.Text);
    cmd.Parameters.AddWithValue("@email", txtemail2.Text);
    cmd.Parameters.AddWithValue("@telefon", txttelefon2.Text);
    cmd.Parameters.AddWithValue("@miasto", txtmiasto2.Text);
    cmd.Parameters.AddWithValue("@id", ""); //set proper id
    cmd.ExecuteNonQuery();
    GridView3.DataBind();
}

Upvotes: 2

Related Questions