Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In my application i am using windows form c# and connected to sql server 2008 r2 with visual studio 2013 and login successfully. Anytime i try to get data from database using the update button , the program returned "an unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"
Can anyone help me, please?

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BankApp
{
    public partial class InsertUpdateDelete : Form
    {
        SqlConnection con = new SqlConnection("Data Source=Aydotcom-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True");
        SqlCommand cmd;
        SqlDataAdapter adapt;
        //ID variable used in Updating and Deleting Record  
        int ID = 0;

        public InsertUpdateDelete()
        {
            InitializeComponent();
            DisplayData();
        }

        private void InsertUpdateDelete_Load(object sender, EventArgs e)
        {

        }

        private void btn_Insert_Click(object sender, EventArgs e)
        {
            if (txt_UName.Text != "" && txt_Password.Text != "")
            {
               // cmd = new SqlCommand("insert into login (UserName,Password,ID) values(@username,@password,@ID)", con);
               cmd = new SqlCommand("insert into login (UserName,Password) values(@username,@password)", con);
               con.Open();
                //cmd.Parameters.AddWithValue("@ID", txt_ID.Text);
                cmd.Parameters.AddWithValue("@username", txt_UName.Text);
                cmd.Parameters.AddWithValue("@password", txt_Password.Text);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Record Inserted Successfully");
                DisplayData();
                ClearData();
            }
            else
            {
                MessageBox.Show("Please Provide Details!");
            }
        }
        //Display Data in DataGridView  
        private void DisplayData()
        {
            con.Open();
            DataTable dt = new DataTable();
            adapt = new SqlDataAdapter("select * from login", con);
            adapt.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
        //Clear Data  
        private void ClearData()
        {
            txt_UName.Text = "";
            txt_Password.Text = "";
            ID = 0;
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            if (txt_UName.Text != "" && txt_Password.Text != "")
            {
                cmd = new SqlCommand("update login set UserName=@Username,Password=@password where ID=@id", con);
                con.Open();
                cmd.Parameters.AddWithValue("@id", ID);
                cmd.Parameters.AddWithValue("@Username", txt_UName.Text);
                cmd.Parameters.AddWithValue("@password", txt_Password.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Updated Successfully");
                con.Close();
                DisplayData();
                ClearData();
            }
            else
            {
                MessageBox.Show("Please Select Record to Update");
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (ID != 0)
            {
                cmd = new SqlCommand("delete login where ID=@id", con);
                con.Open();
                cmd.Parameters.AddWithValue("@id", ID);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Record Deleted Successfully!");
                DisplayData();
                ClearData();
            }
            else
            {
                MessageBox.Show("Please Select Record to Delete");
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {            ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            // ID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            txt_UName.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            txt_Password.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

        }

    }
}
Posted
Updated 26-Apr-17 9:23am
Comments
ZurdoDev 26-Apr-17 13:28pm    
We can help you if you give us the exact error and the line of code that causes the error.

Base on the very little information you did give, I'll be this
dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()
is not an integer. All you have to do is debug the code and you'll see what is happening.
CHill60 26-Apr-17 13:29pm    
Which line causes the problem?
To convert an integer to a string use x.ToString () where x is your integer
[no name] 26-Apr-17 13:31pm    
And you expect us to do what? Learn how to use the debugger to debug your code. Then you won't be expecting random strangers on the internet to debug you code for you when they can't run your code to begin with.

1 solution

As RyanDev[^] mentioned in the comment to the question, you have to debug your programme.

I'd suggest to read this: Troubleshooting Exceptions: System.FormatException[^] to be able to resolve your issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900