Insert Into with parameter

R

Roy Gourgi

Hi,

I am trying to insert a record into my database by using parameters, but I
get the error message System.Data.SqlClient.SqlException. Here is my code
below.

TIA

Roy

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Text;

namespace testing

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

int lnSOBN=900;

int lnBN1=88;

string strConnection = "Server=(local);Integrated
Security=yes;database=royDB";

SqlConnection conn = new SqlConnection(strConnection);


conn.Open();




StringBuilder strInsert = new StringBuilder("insert into tblRoy ");

strInsert.Append("(SOBN, BN1)");

strInsert.Append(" VALUES (@par0, @par1)");

SqlCommand c = new SqlCommand(strInsert.ToString(), conn);

c.Parameters.Add(new SqlParameter("par0", lnSOBN));

c.Parameters.Add(new SqlParameter("par1", lnBN1));

c.ExecuteNonQuery();

conn.Close();






}

}

}
 
R

Roy Gourgi

Thanks it worked.

Roy

imperugo said:
try this code :

string sql = "INSERT INTO [tblRoy ] (SOBN, BN1) VALUES (@par0, @par1) ";

SqlCommand cmd;

using (cmd= new SqlCommand(sqlstr,Conn))

{
SqlParameter parameter1= new SqlParameter
("@par0",SqlDbType.VarChar,255);
SqlParameter parameter2= new SqlParameter
("@par1",SqlDbType.VarChar,255);

parameter1.Value = "the value";
parameter2.Value = "the value";

cmd.Parameters.add(parameter1);
cmd.Parameters.add(parameter2);

cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}


byez
--
imperugo (exCartman)
myblog : http://imperugo.blogspot.com


Roy Gourgi said:
Hi,

I am trying to insert a record into my database by using parameters, but
I get the error message System.Data.SqlClient.SqlException. Here is my
code below.

TIA

Roy

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Text;

namespace testing

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

int lnSOBN=900;

int lnBN1=88;

string strConnection = "Server=(local);Integrated
Security=yes;database=royDB";

SqlConnection conn = new SqlConnection(strConnection);


conn.Open();




StringBuilder strInsert = new StringBuilder("insert into tblRoy ");

strInsert.Append("(SOBN, BN1)");

strInsert.Append(" VALUES (@par0, @par1)");

SqlCommand c = new SqlCommand(strInsert.ToString(), conn);

c.Parameters.Add(new SqlParameter("par0", lnSOBN));

c.Parameters.Add(new SqlParameter("par1", lnBN1));

c.ExecuteNonQuery();

conn.Close();






}

}

}
 
I

imperugo

try this code :

string sql = "INSERT INTO [tblRoy ] (SOBN, BN1) VALUES (@par0, @par1) ";

SqlCommand cmd;

using (cmd= new SqlCommand(sqlstr,Conn))

{
SqlParameter parameter1= new SqlParameter
("@par0",SqlDbType.VarChar,255);
SqlParameter parameter2= new SqlParameter
("@par1",SqlDbType.VarChar,255);

parameter1.Value = "the value";
parameter2.Value = "the value";

cmd.Parameters.add(parameter1);
cmd.Parameters.add(parameter2);

cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}


byez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top