I'm working on a Windows forms app, I created a room form which contains room name, size, and LAB.
The table in the database contains columns RoomName
( varchar
), Size
( int
), and LAB
( bit
).
This is the ADD_ROOM
stored procedure:
CREATE PROCEDURE [dbo].[ADD_ROOM]
@NAME_ROOM VARCHAR(50),
@SIZE INT,
@LAB BIT
AS
INSERT INTO [dbo].[ROOM] ([NAME_ROOM], [SIZE], [LAB])
VALUES (@NAME_ROOM, @SIZE, @LAB)
Here is ADD_ROOM.cs
:
class CLS_ADD_ROOM
{
public void ADD_ROOM(string NAME_ROOM,int SIZE,Boolean LAB)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@NAME_ROOM", SqlDbType.VarChar, 50);
param[0].Value = NAME_ROOM;
param[1] = new SqlParameter("@SIZE", SqlDbType.Int);
param[1].Value = SIZE;
param[2] = new SqlParameter("@LAB", SqlDbType.Bit);
param[2].Value = LAB;
DAL.ExecuteCommand("ADD_ROOM", param);
DAL.Close();
}
public void UPDATE_ROOM(string ID_ROOM, string NAME_ROOM, int SIZE, bool LAB)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[4];
param[0] = new SqlParameter("@ID_ROOM", SqlDbType.Int);
param[0].Value = ID_ROOM;
param[1] = new SqlParameter("@NAME_ROOM", SqlDbType.VarChar, 50);
param[1].Value = NAME_ROOM;
param[2] = new SqlParameter("@SIZE", SqlDbType.Int);
param[2].Value = SIZE;
param[3] = new SqlParameter("@LAB", SqlDbType.Bit);
param[3].Value = LAB;
DAL.ExecuteCommand("UPDATE_ROOM", param);
DAL.Close();
}
And the code behind button ADD Room
in ADD ROOM FORM
private void button1_Click(object sender, EventArgs e)
{
ROOM.ADD_ROOM(TXT_ROOM_NAME.Text, Convert.ToInt32(TXT_ROOM_SIZE.Text), Convert.ToBoolean(CHECK_LAB.CheckState));
MessageBox.Show(" The Room has been added successfully", "ADD PROCEDURE", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.dataGridView1.DataSource = ROOM_VIEW.GET_ALL_ROOMS();
}
And the error is
Input string was not in a correct format
In your C# code, you should replace Convert.ToBoolean(CHECK_LAB.CheckState)
with CHECK_LAB.Checked
- that's the boolean with defines whether or not the checkbox is checked:
private void button1_Click(object sender, EventArgs e)
{
ROOM.ADD_ROOM(TXT_ROOM_NAME.Text,
Convert.ToInt32(TXT_ROOM_SIZE.Text),
CHECK_LAB.Checked);
MessageBox.Show(" The Room has been added successfully", "ADD PROCEDURE", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.dataGridView1.DataSource = ROOM_VIEW.GET_ALL_ROOMS();
}
The CheckState
property is not a boolean property - it's one of three possible values:
CheckState.Checked
CheckState.Unchecked
CheckState.Indeterminate
Not sure, but this could be worth trying. In ADD_ROOM.cls:
param[2] = new SqlParameter("@LAB", SqlDbType.Bit);
param[2].Value = LAB ? 1 : 0; // <---- CHANGE THIS LINE
Value of Boolean as parameter may be converted to something other than 0 or 1, which are expected by sql server.
Thank you so much Guys .. the problem is solved :)
I changed this instruction in ADD_ROOM.cls :
class CLS_ADD_ROOM
{
public void ADD_ROOM(string NAME_ROOM,int SIZE,int LAB)// <=== Change bool to int
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@NAME_ROOM", SqlDbType.VarChar, 50);
param[0].Value = NAME_ROOM;
param[1] = new SqlParameter("@SIZE", SqlDbType.Int);
param[1].Value = SIZE;
param[2] = new SqlParameter("@LAB", SqlDbType.Bit); //<=== keep it .bit
param[2].Value = LAB ;
DAL.ExecuteCommand("ADD_ROOM", param);
DAL.Close();
}
And add this condition in ADD_ROOM Form :
private void button1_Click(object sender, EventArgs e)
{
if (CHECK_LAB.Checked)
{
ROOM.ADD_ROOM(TXT_ROOM_NAME.Text,int.Parse(TXT_ROOM_SIZE.Text),Convert.ToInt32(1));
}
else
{
ROOM.ADD_ROOM(TXT_ROOM_NAME.Text, Convert.ToInt32(TXT_ROOM_SIZE.Text), Convert.ToInt32(0)); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.