473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Insert Into with parameter

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();


}

}

}


Nov 17 '05 #1
2 1877
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" <ro***@videotron.ca> wrote in message
news:Rh********************@weber.videotron.net...
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();


}

}

}



Nov 17 '05 #2
Thanks it worked.

Roy

"imperugo" <ug**********@dgsworld.it> wrote in message
news:eB**************@TK2MSFTNGP09.phx.gbl...
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" <ro***@videotron.ca> wrote in message
news:Rh********************@weber.videotron.net...
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();


}

}

}




Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
4
by: nicholas | last post by:
Could someone tell me how to implement an INSERT on an aspx-page using a stored procedure, in VB-code? THX, Nic PS: I have been trying this, but it ain't working: Sub insert_new_content...
7
by: Kevin Lawrence | last post by:
Hi all I want to do "INSERT INTO Table (Blob) Values('blobdataasstring')". ...rather than using the parameter driven method, is it possible? And if so what encoder do I use to convert the...
4
by: Mark Olbert | last post by:
I am struggling with trying to retrieve the value of an autoincrement identity field after a DetailsView Insert operation. The DetailsView is bound to an SqlDataSource control. So far as I can...
8
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
2
by: shapper | last post by:
Hello, I am working with a ListView but I suppose that with a GridView might be the same. Instead of having an Insert Button on each GridView row I would like to have only one Insert button,...
0
by: troydixon | last post by:
Hello, I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the...
0
by: Michael | last post by:
I know I am missing something simple, but I am stuck. When I submit the following for the employee info gets stored in scale1 and scale1 shows the employee number. Can someone show me my...
0
by: bgreer5050 | last post by:
I know I am missing something simple, but I am stuck. When I submit the following for the employee info gets stored in scale1 and scale1 shows the employee number. Can someone show me my...
2
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.