Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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);
 
<pre>
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
 
        da.Fill(ds, "membership"); 
        //cmd.ExecuteNonQuery();

        //con.Close(); 
       MessageBox.Show("inserted succefully"); 
    }</pre>
<b> below my procedure </b>
 
 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 5:03am
Edited 26-Aug-12 5:34am
Wes Aday94.3K
v2
Comments
Wes Aday at 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 at 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 at 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 at 27-Aug-12 11:58am
   
it is showing same error .Fill: SelectCommand.Connection property has not been initialized. how can i solve this
__TR__ at 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);
 
<pre>
        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
0 OriginalGriff 455
1 Sergey Alexandrovich Kryukov 445
2 Maciej Los 290
3 ProgramFOX 275
4 CHill60 225
0 OriginalGriff 550
1 Sergey Alexandrovich Kryukov 470
2 Maciej Los 310
3 ProgramFOX 275
4 Peter Leow 250


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 27 Aug 2012
Copyright © CodeProject, 1999-2015
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