Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am facing problem in setting number of book present in the database is 0 then show "No Book Is Available " message otherwise if value is 5 then decrement the counter one by one in window application.


Please Reply me soon.. Its urgent

What I have tried:

private void btnSave_Click(object sender, EventArgs e)
{

textBox1.Text = getuniqueID();
SqlConnection con = new SqlConnection(Main.connectionname());

SqlCommand cmd = new SqlCommand();




string quiry1 = "select NoOfBook from BookEntry where AccessionNo='" + comboBox2.Text + "' and NoOfBook <=0";


MessageBox.Show("Book is not available for issue", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);



string quiry = "BEGIN TRY " +
"BEGIN TRANSACTION " +
"INSERT INTO [BookIssue]([T_Id],[issue_date],[due_date],[S_Id],[S_Name],[course],[department],[Accession_no],[book_title],[author],[ISBN],[edition])" +
"VALUES(@T_Id,@issue_date,@due_date,@S_Id,@S_Name,@course,@department,@Accession_no,@book_title,@author,@ISBN,@edition)";




string update = "UPDATE [BookEntry] SET [NoOfBook] = [NoOfBook] - 1 WHERE AccessionNo = '" + comboBox2.Text + "' And NoOfBook >0";

cmd.Parameters.AddWithValue("@T_Id", textBox1.Text.Trim());
cmd.Parameters.AddWithValue("@issue_date", dateTimePicker1.Text.Trim());
cmd.Parameters.AddWithValue("@due_date", dateTimePicker1.Text.Trim());
cmd.Parameters.AddWithValue("@S_Id", comboBox1.Text.Trim());
cmd.Parameters.AddWithValue("@S_Name", textBox3.Text.Trim());
cmd.Parameters.AddWithValue("@course", textBox4.Text.Trim());
cmd.Parameters.AddWithValue("@department", textBox5.Text.Trim());
cmd.Parameters.AddWithValue("@Accession_no", comboBox2.Text.Trim());
cmd.Parameters.AddWithValue("@book_title", textBox7.Text.Trim());
cmd.Parameters.AddWithValue("@author", textBox8.Text.Trim());
cmd.Parameters.AddWithValue("@ISBN", textBox9.Text.Trim());
cmd.Parameters.AddWithValue("@edition", textBox10.Text.Trim());


quiry += update;

quiry += " COMMIT TRANSACTION " +
"END TRY " +
"BEGIN CATCH " +
"ROLLBACK TRANSACTION " +
" END CATCH";

cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = quiry;

try
{
con.Open();//establishes phy link between App and DB
cmd.ExecuteNonQuery();
MessageBox.Show("Information Saved.", "Library Application", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

catch (SqlException ex)
{

MessageBox.Show(ex.Message);
}

finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
Posted
Updated 17-Jul-16 20:23pm
Comments
nilesh sawardekar 18-Jul-16 2:04am    
NoOfBook datafield?
ADI@345 18-Jul-16 2:22am    
NoOfBook is Saved in database that is Int .
I am usng this code
string quiry1 = "select NoOfBook from BookEntry where AccessionNo='" + comboBox2.Text + "' and NoOfBook <=0";

This code gives record 0 .

and

string update = "UPDATE [BookEntry] SET [NoOfBook] = [NoOfBook] - 1 WHERE AccessionNo = '" + comboBox2.Text + "' And NoOfBook >0";
this code decrement NoOfbook greater than 0


but i am not able to combine of these query

1 solution

As per your code which you have provided, you only have define and declare query1 but never used it in sqlcommand to execute. If you are not executing any query then how can you get result to compare.
Also if you have written query1 for messagebox then that query also is not proper. Your query should be like below,

C#
string quiry1 = "select COUNT(*) from BookEntry where AccessionNo='" + comboBox2.Text + "' and NoOfBook <=0";


C#
SqlCommand cmd=new SqlCommand(query1, con);
SqlDataReader dr1 ;
dr1=cmd.ExecuteReader();
if (cmd.ExecuteReader().RecordsAffected > 0)
{
    if ((int)cmd.ExecuteScalar()<=0)
    {
        MessageBox.Show("Book is not available for issue", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
cmd.ExecuteReader().Close();
cmd.Dispose();
 
Share this answer
 
Comments
ADI@345 18-Jul-16 2:34am    
how could i merge both query ...

Actually i have to do this
when number of book present in the database is 0 then show "No Book Is Available " message otherwise if value is 5 then decrement the counter one by one ..
ADI@345 18-Jul-16 2:58am    
I am using above code provided by you but this is giving error,

ExecuteReader requires an open and available Connection. The connection's current state is closed.

how to solve this issue..
nilesh sawardekar 18-Jul-16 3:25am    
open connection first, like you did in your code,
SqlConnection con = new SqlConnection(Main.connectionname());
before this code.

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