asp.net webform - I am trying to use query result (integer) as parameter for another sql query but I don't manage to pass the result to the next query. The parameter I try to pass is txtPMSNo which I get from first query and try to use in the second whitout putting it in a view
protected void AddNewPMS(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connString);
{
int PMSNo;
int txtPMSNo;
string sqlQuery = "SELECT Max(nrPMS) FROM PMSmain";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
cmd.CommandType = CommandType.Text;
conn.Open();
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result > 0)
{
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
PMSNo = rdr.GetInt32(0);
if (PMSNo == 0)
{
txtPMSNo = 1;
}
else
{
txtPMSNo = PMSNo + 1;
}
}
}
}
conn.Close();
}
string sqlQueryADD = "IF NOT EXISTS (SELECT nrPMS, dataPMS FROM PMSmain) INSERT INTO PMSmain (nrPMS, dataPMS) VALUES (@PMSnr, @PMSData)";
using (SqlCommand cmd = new SqlCommand(sqlQueryADD, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@PMSnr", txtPMSNo);
DateTime tmpPMSData = DateTime.Now.Date;
cmd.Parameters.AddWithValue("@PMSData", tmpPMSData);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
Response.Redirect("/Pages/IssuePMS.aspx?PMSNo=" + txtPMSNo, false);
}
}
Marius VasilePosted Apr 2, 2023, 2:18 PM
Got it to work, damn, plain sql is much harder than EF :)
Marius VasilePosted Apr 2, 2023, 2:15 PM
There may be a problem in the next query
result shouldn't be exactly the query result? I mean a number? So the query should look like
I no longer have the error but the parameter is still not correct
Marius VasilePosted Apr 2, 2023, 2:00 PM
I tried that but then it will always use that 0 in the second query.I thought of storing txtPMSNo value in a hidden field? but then I will have this kind of hidden fields for all passing parameters?
Tuhin PaulPosted Apr 2, 2023, 1:57 PM
May be one way to solve this would be to initialize txtPMSNo to a default value outside of the if (result > 0) block, like so
within the if (result > 0) block, you can set the value of txtPMSNo based on the value of PMSNo
This way, txtPMSNo will always have a value before it is used as a parameter in the second query.
Marius VasilePosted Apr 2, 2023, 1:49 PM
the error I have is "Use of unassigned local variable txtPMSNo"
Tuhin PaulPosted Apr 2, 2023, 1:47 PM
Can you post the error?
Marius VasilePosted Apr 2, 2023, 1:44 PM
still, the error I have is "Use of unassigned variable". cmd is not an issue since I closed the first one, the parameter is not recognized
Marius VasilePosted Apr 2, 2023, 1:41 PM
Thank you Tuhin Paul for answer. I open and close connections for both querries and even if I split the query into its own procedure, the problem I have is passing the parameter
Tuhin PaulPosted Apr 2, 2023, 1:40 PM
I created a new SqlCommand object named cmd2 for the second query, and passed the txtPMSNo parameter to it using cmd2.Parameters.AddWithValue("@PMSnr", txtPMSNo).
Tuhin PaulPosted Apr 2, 2023, 1:39 PM
I think you're trying to execute the second query using the same SqlCommand object from the first query, which is not possible since the connection is already open and in use. Instead, you should create a new SqlCommand object for the second query and pass the parameter to it.