Mr. Learner Mr. Learner - 2 years ago 73
ASP.NET (C#) Question

passing value to SQL command parameters error

Is anyone can tell me, why I'm getting error here.
If pass a value to parameters with this line

int qID = 10087;


It works fine, but when I try this line

int qID = Int32.Parse(corr[0]);


It gives me error (failed), rest of code is same in both situations. What could be a problem here?

int pID1 = 102;
//int qID = 10087;
int qID = Int32.Parse(corr[0]);
using (var cn1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["lConn"].ConnectionString))
{
cn1.Open();
string sql1 = "SELECT DISTINCT Q FROM QATXT WHERE S= @pID AND QID=@qID ";
SqlCommand cmd1 = new SqlCommand(sql1, cn1);
cmd1.Parameters.AddWithValue("@pID", pID1);
cmd1.Parameters.AddWithValue("@qID", qID);
SqlDataReader rdr1 = cmd1.ExecuteReader();
if (rdr1.Read())
{
mainQTxt = (string)rdr1.GetValue(0);
}

cn1.Close(); rdr1.Close(); cmd1.Dispose();
}//- End of cn


rep = mainQTxt ;

Answer Source

Try Changing int qID = Int32.Parse(corr[0]); to int qID = Convert.ToInt32(corr[0]);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download