Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
<Hi Friends,

I need a small help. I have written a function to insert values into DB. Function is given Below
C#
public bool insertfamily(OleDbCommand cmd)
       {
           if (conn.State == ConnectionState.Closed)
           { conn.Open(); }
           string strqry = "INSERT INTO Family(Hno,Hname,Landphone,FullName,Mobile,DOB,Status,Gender)" +                          " Values(@HNO,@Hname,@Landphone,@Name,@Mobile,@Dob,@Status,@Gender)";
           cmd = new OleDbCommand(strqry, conn);
           var resultSet = cmd.ExecuteNonQuery();
           if (resultSet.Equals(0))
           {
               MessageBox.Show("Records not inserted.please check", "My Application");
               return false;
           }
           return true;
       }

My Requirement is to pass the parameter list to the function
C#
databaseclass dc = new databaseclass();
               OleDbCommand cmd = new OleDbCommand();
               cmd.Parameters.AddWithValue("HNO", txthouseno.Text);
               cmd.Parameters.AddWithValue("Hname", txtHouseName.Text);
               cmd.Parameters.AddWithValue("Landphone", txtland.Text);
               cmd.Parameters.AddWithValue("Name", txtname.Text);
               cmd.Parameters.AddWithValue("Mobile", txtmobile.Text);
               cmd.Parameters.AddWithValue("Dob", mtxtdob.Text);
               cmd.Parameters.AddWithValue("Status", cmbstatus.Text);
               cmd.Parameters.AddWithValue("Gender", cmbgender.Text);
               dc.insertfamily(cmd);


How can i achieve this? What is the best Method?
Posted
Updated 3-Jan-15 20:48pm
v2

Change
C#
cmd.Parameters.AddWithValue("HNO", txthouseno.Text);

to
C#
cmd.Parameters.AddWithValue("@HNO", txthouseno.Text);

and so on...

It should work as you have done it.

[UPDATE]
C#
public bool insertfamily(OleDbCommand cmd)
{
    if (conn.State == ConnectionState.Closed)
    { conn.Open(); }
    string strqry = "INSERT INTO Family(Hno,Hname,Landphone,FullName,Mobile,DOB,Status,Gender)" +                          " Values(@HNO,@Hname,@Landphone,@Name,@Mobile,@Dob,@Status,@Gender)";
    cmd = new OleDbCommand(strqry, conn);
    cmd.Connection = conn;
    cmd.CommandText = strqry;
    cmd.CommandType = CommandType.Text;  // Default
    var resultSet = cmd.ExecuteNonQuery();
    if (resultSet.Equals(0))
    {
        MessageBox.Show("Records not inserted.please check", "My Application");
        return false;
    }
    return true;
}
;
 
Share this answer
 
v2
Comments
jinesh sam 4-Jan-15 3:13am    
@George Thanks for ur response. My question was different. I need to accept the parameter list in the function. in the function i am using <bold>cmd = new OleDbCommand(strqry, conn); so my parameter collection is lost
George Jonsson 4-Jan-15 3:21am    
And why do you do that if you send 'cmd' as a parameter?
jinesh sam 4-Jan-15 5:14am    
Thanks....
create method to accept list of parameters and then you can send the parameter list when you calling that method. for example
C#
public static int ExecuteSqlStatement(string sqlStr, List<OleDbParameter> parameters)
{
            using(OleDbConnection conn = new OleDbConnection(connectionString))
	    using(OleDbCommand cmd = new OleDbCommand(sqlStr, conn))
            {
              if (parameters!= null)
              {
                  foreach (OleDbParameter parm in parameters)
                  {
                      cmd.Parameters.Add(parm);
                  }
              }
              con.Open();
	      return cmd.ExecuteNonQuery();
            }
}
 
Share this answer
 
v2
Comments
jinesh sam 4-Jan-15 3:16am    
@Damith Thanks... Can you please help me to create List<OleDbParameter>

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