Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am continoulsy getting error like , iunable to solve , please anybody help me

C#
static string constring = ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;
         static SqlConnection con = new SqlConnection(constring);
        private void button1_Click(object sender, EventArgs e)
        {
            //con.Open();
           SqlCommand cmd = new SqlCommand("in_mem");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@sl_no",txtslno.Text); 
            cmd.Parameters.AddWithValue("@MemNo",txtmbm.Text);
            cmd.Parameters.AddWithValue("@name", txtnomname.Text);
            cmd.Parameters.AddWithValue("@deptno", txtdept.Text);
            cmd.Parameters.AddWithValue("@empno", txtemp.Text) ;
            cmd.Parameters.AddWithValue("@d_o_b", dateTimePicker1.Value) ;
            cmd.Parameters.AddWithValue("@d_o_m", dateTimePicker2.Value) ;
            cmd.Parameters.AddWithValue("@d_o_r",dateTimePicker3.Value) ;
            cmd.Parameters.AddWithValue("@nomi_name",txtnomname.Text) ;
            cmd.Parameters.AddWithValue("@relationship", txtrelation.Text) ;
            cmd.Parameters.AddWithValue("@address", txtaddr.Text);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();

            da.Fill(ds, "membership"); 
            //cmd.ExecuteNonQuery();

            //con.Close(); 
           MessageBox.Show("inserted succefully"); 
        }

below my procedure

SQL
 CREATE procedure in_mem(    
@sl_no numeric(18,0),    
@MemNo numeric(18,0),          
@name varchar(50),          
@deptno numeric(18,0),          
@empno numeric(18,0),          
@d_o_b date,          
@d_o_m date,          
@d_o_r date,          
@nomi_name varchar(50),          
@relationship varchar(50),          
@address varchar(50)          
)          
as           
begin           
insert into membership(sl_no,MemNo,name,deptno,empno,d_o_m,d_o_b,d_o_r,          
nomi_name,          
relationship,          
address          
)           
values(    
@sl_no,    
@MemNo,          
@name,          
@deptno,          
@empno,          
convert(varchar(10),@d_o_b,103),          
convert(varchar(10),@d_o_m,103),          
convert(varchar(10),@d_o_r,103),         
@nomi_name,          
@relationship,          
@address          
)          
end
Posted
Updated 26-Aug-12 4:34am
v2
Comments
[no name] 26-Aug-12 10:23am    
The error is perfectly clear. Your connection property on your command object has not been initialized. What is the problem?
amperayani 27-Aug-12 11:57am    
where it is ? please tell me

Try This. It will run. Reply about result.
C#
static string constring = ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;
         static SqlConnection con = new SqlConnection(constring);
        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("in_mem",con);
            
            cmd.CommandText = "in_mem";
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.AddWithValue("@sl_no",txtslno.Text); 
            cmd.Parameters.AddWithValue("@MemNo",txtmbm.Text);
            cmd.Parameters.AddWithValue("@name", txtnomname.Text);
            cmd.Parameters.AddWithValue("@deptno", txtdept.Text);
            cmd.Parameters.AddWithValue("@empno", txtemp.Text) ;
            cmd.Parameters.AddWithValue("@d_o_b", dateTimePicker1.Value) ;
            cmd.Parameters.AddWithValue("@d_o_m", dateTimePicker2.Value) ;
            cmd.Parameters.AddWithValue("@d_o_r",dateTimePicker3.Value) ;
            cmd.Parameters.AddWithValue("@nomi_name",txtnomname.Text) ;
            cmd.Parameters.AddWithValue("@relationship", txtrelation.Text) ;
            cmd.Parameters.AddWithValue("@address", txtaddr.Text);
 
            
            
            cmd.ExecuteNonQuery();

            con.Close(); 

           MessageBox.Show("inserted succefully"); 
        }
 
Share this answer
 
As mentioned in the above solution, reason for error is the connection property on your command object is not initialized.
One way of initialising the connection property is
C#
cmd.Connection = con;


Also take a look at this article. It might be helpful
Beginners guide to accessing SQL Server through C#[^]
 
Share this answer
 
Comments
amperayani 27-Aug-12 11:58am    
it is showing same error .Fill: SelectCommand.Connection property has not been initialized. how can i solve this
__TR__ 27-Aug-12 12:19pm    
Can you post your updated code.
C#
SqlDataAdapter da = new SqlDataAdapter(cmd, con);

DataSet ds = new DataSet();


add con reference like above
 
Share this answer
 
Comments
amperayani 27-Aug-12 11:56am    
Again same error it is not accepting
SqlDataAdapter da = new SqlDataAdapter(cmd, con); it is shwoing error

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