Click here to Skip to main content
12,450,977 members (25,406 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
i am continoulsy getting error like , iunable to solve , please anybody help me

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

 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 26-Aug-12 4:03am
Updated 26-Aug-12 4:34am
Wes Aday104.2K
v2
Comments
Wes Aday 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

add con reference like above
  Permalink  
Comments
amperayani 27-Aug-12 11:56am
   
Again same error it is not accepting
SqlDataAdapter da = new SqlDataAdapter(cmd, con); it is shwoing error
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
cmd.Connection = con;

Also take a look at this article. It might be helpful
Beginners guide to accessing SQL Server through C#[^]
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try This. It will run. Reply about result.
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"); 
        }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 27 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100