如何在C#中将整数转换为字符串?
问题描述:
在我的应用程序中,我使用windows form c#并使用visual studio 2013连接到sql server 2008 r2并成功登录。无论何时我尝试使用更新按钮从数据库获取数据,程序返回mscorlib.dll中发生了'System.FormatException'类型的未处理异常
任何人都可以帮助我吗?
我尝试过:
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();
}
}
}
答
as RyanDev [ ^ ]在问题的评论中提到,你必须调试你的程序。
我建议阅读:排除异常故障:System.FormatException [ ^ ]能够解决您的问题。
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.