last day (15 days later) » 

2:34 PM
1
A: Make the method parameterized

medaYou can change your function a little bit: public SqlDataReader executeQuerys(string query01, string paramName, string value) { SqlConnection con = null; SqlCommand com = null; try { con = new SqlConnection(DBConnect.makeConnection()); con.Open(); com = ne...

 
@Silvermind I expanded on my answer thanks!
 
@meda it gives me this error, Error DataAccessLayer.executeQuerys(string, string, string)': not all code paths return a value
 
@chathwind it makes sense, check my edit, the latter function
 
@meda thanks it works.
 
@chathwind you are welcome, good luck!
 
2:34 PM
@meda When I try to read the values in the data reader in the form code like this it SqlDataReader reader = new CandidateOP().getLGDivID(cmbLgDiv.Text); if (reader.HasRows) { while (reader.Read()) { templgDivID = (Int32)reader[0]; } reader.Close(); } It gives me the error Invalid attempt to call HasRows when reader is closed, but it worked with my previous code. What do you think the reason is..?
OK
 
Can you show me your function
 
Here's my code for reader in FORM.
SqlDataReader reader02 = new CandidateOP().getpartyID(cmbParty.Text);
if (reader02.HasRows)
{
while (reader02.Read())
{
tempPrID = (Int32)reader02[0];
}
reader02.Close();
}
 
public SqlDataReader executeQuerys(string query01, string paramName, string value)
that function
 
yes
It gives me the error Invalid attempt to call HasRows when reader is closed, but it worked with my previous code. What do you think the reason is..?
 
Let me see pl
public SqlDataReader executeQuerys(string query01, string paramName, string value)
please
 
2:43 PM
public SqlDataReader executeQuerys(string query01, string paramName, string value)
{
//SqlConnection con = null;
SqlCommand com = null;
using (SqlConnection con = new SqlConnection(DBConnect.makeConnection()))
{
try
{
con.Open();
com = new SqlCommand(query01, con);
com.Parameters.AddWithValue(paramName, value);

}

catch (SqlException ex)
{
//Handle the exception
//no need to dispose connection
}
return com.ExecuteReader(CommandBehavior.CloseConnection);
}
}
 
public SqlDataReader executeQuerys(string query01, string paramName, string value)
{
    SqlConnection con = null;
    SqlCommand com = null;
    try
    {
        con = new SqlConnection(DBConnect.makeConnection());
        con.Open();
        com = new SqlCommand(query01, con);
        com.Parameters.AddWithValue(paramName, value);
    }
    catch
    {
        com.Dispose();
        con.Close();
        throw;
    }
   return com.ExecuteReader(CommandBehavior.CloseConnection);
}
what if you go back to your code\
 
Now it works with this code
can you explain me what was causing error if you don't mind
 
i dont think it s good to return ExecuteReader
 
OK
is that anything to do with using statement?
 
yes
because it is being dispose
 
2:57 PM
Ok thank you very much
 
@chathwind your welcome
remember to accept !
God bless you!
 
OK I have one more question
 
sure
 
If I have a method with no parameter do I have to write this again without including the parameter
Assume I have no parameters and I'm just executing only the stored procedure
 
3:27 PM
maybe you can put it into a dictionary
and add values in a for each
 
How can I do that?
 
 
1 hour later…
4:53 PM
Dictionary<string, string> params = new Dictionary<string, string>()
{
	{"@divName", divName},
	{"@divId", "2324"}
};
then change the method to take a dictionarry
foreach (var param in params)
{
	com.Parameters.AddWithValue(param.Key, param.Value);
	Console.WriteLine("{0}, {1}",
	param.Key,
	param.Value);
}
@chathwind
 
 
5 hours later…
10:05 PM
Thanks
 
 
2 hours later…
11:38 PM
Did that work ?
@chathwind
 

  last day (15 days later) »