Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't Know what is the problem here with this code

the stored proc is :-

SQL
alter proc [dbo].[addrmpbirth]
@bday varchar(50),
@bm varchar(50),
@by varchar(50)
as
begin
insert into Employee (EBirthday,EBirthMonth,EBirthyear) values (@bday,@bm,@by)
end


& C# code is :-

C#
public DataTable Executereadercommand()
        {
            con.Open();
            com.Connection = con;
            SqlDataReader sdr = com.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(sdr);
            con.Close();
            return dt;
        }

        public DataTable addbirthemp(string bd,string bm,string by) 
        {
            com.CommandText = "addrmpbirth";
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Clear();
            com.Parameters.AddWithValue("@bday",bd);
            com.Parameters.AddWithValue("@bm",bm);
            com.Parameters.AddWithValue("@by",by);
          DataTable dt=  Executereadercommand();
          return dt;
        }
Posted
Comments
Sergey Alexandrovich Kryukov 14-Feb-13 19:09pm    
Please don't post questions, updates, comments and the like as "Solution". Please understand that it can cause abuse reports. Some critical number of abuse reports can lead to cancellation of CodeProject accounts. A number of heavy abuses already lost their accounts. I understand that you did not try to cheat or something, in this case, but you just need to be careful about posting "solutions".

Please use "Improve question", post comments to any posts, reply to existing comments.

Thank you for understanding
—SA
Mostafa Metwally 14-Feb-13 19:25pm    
i understand that
thank you Mr.Sergey
i'll be carful

Why are you trying to use an INSERT statement with a DataTable Load? That would seem to be a pretty big problem right there.
 
Share this answer
 
Comments
Mostafa Metwally 5-Feb-13 7:07am    
because it's a combobox and it will inseret into birthday column in database
so i used datatable because it's a selected value

was that right ?
Pete O'Hanlon 5-Feb-13 7:39am    
No it's not right. You are attempting to do a load here - not an insert operation. Rather than using a DataTable and a load, a simple ExecuteNonQuery would have done the job. Take out the DataTable part and just call com.ExecuteNonQuery();.
Mostafa Metwally 5-Feb-13 15:53pm    
thank you Mr.pete for your answer
1) In your case, it is a NonQuery statement you want to execute, that does not return a result set.
2) As I see you are trying to reuse some objects (com for example). Don't.

For the start add a com.ExecuteNonQuery(); statement before DataTable dt=Executereadercommand();
 
Share this answer
 
Comments
Mostafa Metwally 5-Feb-13 7:10am    
so that i'll use this

public int ExecuteNonquerycommand()
{
con.Open();
com.Connection = con;
int r=com.ExecuteNonQuery();
con.Close();
return r;
Zoltán Zörgő 5-Feb-13 7:21am    
Don't code like this. You mess up the setting of the objects.

public void addbirthemp(string connectionString, string bd, string bm, string by)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand com = new SqlCommand("addrmpbirth", connection);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Clear();
com.Parameters.AddWithValue("@bday",bd);
com.Parameters.AddWithValue("@bm",bm);
com.Parameters.AddWithValue("@by",by);
com.ExecuteNonQuery();
}
}

Actually the above code will do a little too much separation, but that is better than making useless reuse.
Mostafa Metwally 5-Feb-13 7:12am    
I used ExecuteNonQuery();

and same problem :(
Mostafa Metwally 5-Feb-13 7:36am    
I Already Connected with Database

Collapse | Copy Code
class empreg:Adoconnection
{
SqlConnection con;
SqlCommand com;

public empreg()
{
con = new SqlConnection(GetConnection());
com = new SqlCommand();
}

Collapse | Copy Code
public string GetConnection()
{
return "data source=.;database=Msprogect;integrated security=true;";
}
Zoltán Zörgő 5-Feb-13 7:42am    
Again: don't code like this. It is not a good approach, you mess up the objects. The connection and commands objects are intended to be short-living. The best-practice is: use them for a single transaction.
Read this one, including subpages: http://msdn.microsoft.com/en-us/library/8xx3tyca(VS.80).aspx

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