Anyone tell me why after pressing the CheckOut button it dont go to the CheckIn button after, this is my code, any questions let me know
string myConnectionString = (string) ConfigurationSettings.AppSettings["ConnectionString"];
// create connection
SqlConnection cnn = new SqlConnection(myConnectionString);
// create command
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;
string CheckedOut = "0";
if (CheckedOut == "1")
{
cmd.CommandText = "select * from CheckedOut where CheckedOut = '1'";
CheckOut.Visible = false;
CheckIn.Visible = true;
Console.WriteLine("Please check IN your IR");
}
else
{
cmd.CommandText = "select * from CheckedOut where CheckedOut = '0'";
CheckIn.Visible = false;
CheckOut.Visible = true;
Console.WriteLine("Check OUT IR first");
}
}
Loading
Amy TurnbullPosted Sep 19, 2007, 3:15 AM
string myConnectionString = (string) ConfigurationSettings.AppSettings["ConnectionString"];
// create connection
SqlConnection cnn = new SqlConnection(myConnectionString);
// create command
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BugTrackerWhereCheckedOut";
// add parameters
cmd.Parameters.Add(new SqlParameter("@bg_id", SqlDbType.Int,4));
cmd.Parameters["@bg_id"].Value = Request.QueryString["id"];
cmd.Parameters.Add(new SqlParameter("@us_id", SqlDbType.Int,4));
cmd.Parameters["@us_id"].Value = Convert.ToString(security.this_usid);
cnn.Open();
if (cmd.ExecuteScalar() != null)
{
Response.Write("record");
string strCheckOutorIn = cmd.ExecuteScalar().ToString();
if (strCheckOutorIn == "true")
{
CheckOut.Visible = false;
CheckIn.Visible = true;
Console.WriteLine("Please check IN your IR");
}
else
{
CheckIn.Visible = false;
CheckOut.Visible = true;
Console.WriteLine("Please Check OUT your IR first");
}
// execute and close
}
else
{
Response.Write("No Record");
}
cnn.Close();
}
and the store procedure is the following
ALTER PROCEDURE [dbo].[BugTrackerWhereCheckedOut]
(
@bg_id int,
@us_id int
) AS
select CheckedOut, bg.bg_id as Bug_ID, us_username as Username
from CheckedOut ck inner join bugs bg on bg.bg_id = ck.bg_id
inner join users us on us.us_id = ck.us_id where ck.bg_id = @bg_id and ck.us_id = @us_id
Jan MontanoPosted Sep 18, 2007, 9:41 PM
How did you use ExecuteScalar?
Did you use it like the code below?
//cmd.CommandText = "SELECT COUNT(*) FROM dbo.region";
Int32 count = (Int32) cmd.ExecuteScalar();
In this example, cmd.ExecuteScalar() is cast as Int32 because we're expecting an int value. In your case, I assume you are expecting a return value of 1 or 0 for your field. It's of numeric data type, you could cast it to int or to to the specific numeric data type so that you'll be able to retrieve it's content correctly.
Regards,
Jan
santhoshPosted Sep 18, 2007, 11:42 AM
ExecuteScalar always return an Object ..how get true or false?
Amy TurnbullPosted Sep 18, 2007, 10:23 AM
Amy TurnbullPosted Sep 18, 2007, 9:08 AM
// create connection
SqlConnection cnn = new SqlConnection(myConnectionString);
// create command
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BugTrackerWhereCheckedOut";
// add parameters
cmd.Parameters.Add(new SqlParameter("@bg_id", SqlDbType.Int,4));
cmd.Parameters["@bg_id"].Value = Request.QueryString["id"];
cmd.Parameters.Add(new SqlParameter("@us_id", SqlDbType.Int,4));
cmd.Parameters["@us_id"].Value = Convert.ToString(security.this_usid);
// execute and close
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
So I have run the store porcedure for this code but how can I get the first column value which is Checkedout that shows if its 0 or 1, is it executereader ?
Amy TurnbullPosted Sep 18, 2007, 5:19 AM
Jan MontanoPosted Sep 18, 2007, 5:10 AM
I was just pointing out that whatever you do, it will not go inside the if (CheckedOut == "1") condition. It won't go inside that condition because of your code string CheckedOut = "0"; right just before it. CheckedOut will always be zero when entering your if condition. You should declare it instead as a class-level variable, and not inside a method.
public Class Test
{
}
Amy TurnbullPosted Sep 18, 2007, 3:52 AM
Can you give me an example of a loop condition?
Thanks
Jan MontanoPosted Sep 17, 2007, 9:49 PM
How is the Checkout button click event implemented? You have a string CheckedOut initialized to "0". But after the declaration, you use it in a loop condition? I think it will always enter the else condition. Unless you move the "0" initialization somewhere else. and declare string CheckedOut on class level.