473,395 Members | 1,473 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,395 software developers and data experts.

SqlParameter and Update problem

Hi,

The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is
working for update,
but SqlParameter myPM =new SqlParameter
("@Address",SqlDbType.NVarChar,90,txtAddress.Te xt) is not working.
The Address column is DataType NVarChar 90, no problem.
Any idea?
Jason
Nov 17 '05 #1
5 8269
That is because that constructor is not used for passing in the Value of the
parameter. It is used to pass in the name of the sourceColumn which would be
used when the Update method of the SqlDataAdapter is called (and assuming the
command object which holds this parameter is participating in the update
process)

// constructor
public SqlParameter(string parameterName, SqlDbType dbType, int size, string
sourceColumn)
{

}

you are actually adding in the name of the sourceColumn and NOT the value...

the constructor accepts the value of the parameter in 2 forms:

// #1
public SqlParameter(string parameterName, object value) { .. }

// #2
public SqlParameter(string parameterName, SqlDbType dbType, int size,
ParameterDirection direction, bool isNullable, byte precision, byte scale,
string sourceColumn, DataRowVersion sourceVersion, object value) { .. }
Thus:

SqlParameter parameter = new SqlParameter("@Address",SqlDbType.NVarChar,90);
parameter.Value = txtAddress.Text;

or if you are referring to the actual command object:

command.Parameters.Add("@Address",SqlDbType.NVarCh ar,90).Value =
txtAddress.Text;

Regards,

Brandon
"Jason Huang" wrote:
Hi,

The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is
working for update,
but SqlParameter myPM =new SqlParameter
("@Address",SqlDbType.NVarChar,90,txtAddress.Te xt) is not working.
The Address column is DataType NVarChar 90, no problem.
Any idea?
Jason

Nov 17 '05 #2
Thanks Brandon!
I made up an UpdatByParameter function.
Would you give me some advice to improve my UpdateByParameter function?
strUpdate ="UPDATE ContactAddr SET Serial=@Serial, ";
strUpdate +="ZipCode =@ZipCode, Address=@Address ";
strUpdate +="WHERE CustNo=@CustNo AND Serial=@Serial ";
SqlParameter [] myPM=new SqlParameter[4];
myPM[0] = new SqlParameter("@Serial", SqlDbType.SmallInt, 2);
myPM[1] = new SqlParameter("@ZipCode", SqlDbType.Char, 10);
myPM[2] = new SqlParameter("@Address", SqlDbType.NVarChar, 90);
myPM[3] = new SqlParameter("@CustNo", SqlDbType.NVarChar, 8);
myPM[0].Value=txtSerial.Text;
myPM[1].Value=txtZipCode.Text;
myPM[2].Value=txtAddress.Text;
myPM[3].Value=txtCustNo.Text;
this.UpdatByParameter(strUpdate, myPM, 1);

public void UpdatByParameter(string strSQL, SqlParameter [] GoodParameter,
int i)
{
mConn=new SqlConnection(gstrConn);//new connection need to be here first
mConn.Open();
mComm=new SqlCommand(strSQL,mConn);
for (int j=0; j < i; j ++)
{
mComm.Parameters.Add(GoodParameter [j]); //2005.10.19
}
mTrans = mConn.BeginTransaction();
mComm.Transaction=mTrans;
try
{
mComm.ExecuteNonQuery();
mTrans.Commit();
}
catch(SqlException e)
{
mTrans.Rollback();
}
mConn.Close();
}//end UpdateByParameter

"Brandon Driesen" <Brandon Dr*****@discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D:13********************************* *@microsoft.com...
That is because that constructor is not used for passing in the Value of
the
parameter. It is used to pass in the name of the sourceColumn which would
be
used when the Update method of the SqlDataAdapter is called (and assuming
the
command object which holds this parameter is participating in the update
process)

// constructor
public SqlParameter(string parameterName, SqlDbType dbType, int size,
string
sourceColumn)
{

}

you are actually adding in the name of the sourceColumn and NOT the
value...

the constructor accepts the value of the parameter in 2 forms:

// #1
public SqlParameter(string parameterName, object value) { .. }

// #2
public SqlParameter(string parameterName, SqlDbType dbType, int size,
ParameterDirection direction, bool isNullable, byte precision, byte scale,
string sourceColumn, DataRowVersion sourceVersion, object value) { .. }
Thus:

SqlParameter parameter = new
SqlParameter("@Address",SqlDbType.NVarChar,90);
parameter.Value = txtAddress.Text;

or if you are referring to the actual command object:

command.Parameters.Add("@Address",SqlDbType.NVarCh ar,90).Value =
txtAddress.Text;

Regards,

Brandon
"Jason Huang" wrote:
Hi,

The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is
working for update,
but SqlParameter myPM =new SqlParameter
("@Address",SqlDbType.NVarChar,90,txtAddress.Te xt) is not working.
The Address column is DataType NVarChar 90, no problem.
Any idea?
Jason

Nov 17 '05 #3
assuming this is a collection you create:

MicrosoftSqlParameterCollection.cs
===========================
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.NewsGroups
{

public class MicrosoftSqlParameterCollection : CollectionBase
{

private static bool IsValidType(object value)
{
return (value != null && value is SqlParameter);
}

protected override void OnInsert(int index, object value)
{
if (! IsValidType(value))
throw new InvalidTypeException("Invalid Type");
else
base.OnInsert (index, value);

}

protected override void OnRemove(int index, object value)
{
if (! IsValidType(value))
throw new InvalidTypeException("Invalid Type");
else
base.OnRemove (index, value);

}

protected override void OnSet(int index, object oldValue, object newValue)
{
if (! IsValidType(oldValue) || ! IsValidType(newValue))
throw new InvalidTypeException("Invalid Type");
else
base.OnSet (index, oldValue, newValue);
}

public SqlParameter Add(string parameterName, SqlDbType dbType, object
value)
{
SqlParameter parameter = new SqlParameter(parameterName,dbType);
parameter.Value = value;
Add(parameter);
return parameter;
}

public SqlParameter Add(string parameterName, SqlDbType dbType, int size,
object value)
{
SqlParameter parameter = new SqlParameter(parameterName,dbType,size);
parameter.Value = value;
Add(parameter);
return parameter;
}

public void Add(SqlParameter parameter)
{
this.InnerList.Add(parameter);
}

public void Remove(string parameterName)
{
SqlParameter parameter = FindByParameterName(parameterName);
if (parameter != null)
this.InnerList.Remove(parameter);
}

public SqlParameter this [int index]
{
get { return (SqlParameter)this.InnerList[index]; }
}

public SqlParameter this [string parameterName]
{
get { return FindByParameterName(parameterName); }
}

private SqlParameter FindByParameterName(string value)
{
foreach(SqlParameter item in this.InnerList)
{
if (string.Compare(item.ParameterName,value,true) == 0)
return item;
}
return null;
}

public SqlParameter [] ToArray()
{
return (SqlParameter [])this.InnerList.ToArray(typeof(SqlParameter));
}

}

public class InvalidTypeException : Exception
{
public InvalidTypeException(string message) : base(message) {}
public InvalidTypeException(string message,Exception innerException) :
base(message,innerException) {}
public InvalidTypeException() : base() {}
}
}

The code
======

public void UpdateByParameter(string sql, SqlParameter [] parameters)
{
UpdateByParameter(sql,parameters);
}

public void UpdateByParameter(string sql, MicrosoftSqlParameterCollection
parameters)
{
UpdateByParameter(sql,parameters);
}

private void InternalUpdateByParameter(string sql, ICollection parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(sql,connection))
{
connection.Open();
using (SqlTransaction transaction = connection.BeginTransaction())
{
command.Transaction = transaction;
if (parameters != null)
{
foreach(SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
command.ExecuteNonQuery();
transaction.Commit();
}
}
}
}
"Jason Huang" <Ja************@hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP15.phx.gbl...
Thanks Brandon!
I made up an UpdatByParameter function.
Would you give me some advice to improve my UpdateByParameter function?
strUpdate ="UPDATE ContactAddr SET Serial=@Serial, ";
strUpdate +="ZipCode =@ZipCode, Address=@Address ";
strUpdate +="WHERE CustNo=@CustNo AND Serial=@Serial ";
SqlParameter [] myPM=new SqlParameter[4];
myPM[0] = new SqlParameter("@Serial", SqlDbType.SmallInt, 2);
myPM[1] = new SqlParameter("@ZipCode", SqlDbType.Char, 10);
myPM[2] = new SqlParameter("@Address", SqlDbType.NVarChar, 90);
myPM[3] = new SqlParameter("@CustNo", SqlDbType.NVarChar, 8);
myPM[0].Value=txtSerial.Text;
myPM[1].Value=txtZipCode.Text;
myPM[2].Value=txtAddress.Text;
myPM[3].Value=txtCustNo.Text;
this.UpdatByParameter(strUpdate, myPM, 1);

public void UpdatByParameter(string strSQL, SqlParameter []
GoodParameter, int i)
{
mConn=new SqlConnection(gstrConn);//new connection need to be here first
mConn.Open();
mComm=new SqlCommand(strSQL,mConn);
for (int j=0; j < i; j ++)
{
mComm.Parameters.Add(GoodParameter [j]); //2005.10.19
}
mTrans = mConn.BeginTransaction();
mComm.Transaction=mTrans;
try
{
mComm.ExecuteNonQuery();
mTrans.Commit();
}
catch(SqlException e)
{
mTrans.Rollback();
}
mConn.Close();
}//end UpdateByParameter

"Brandon Driesen" <Brandon Dr*****@discussions.microsoft.com>
¼¶¼g©ó¶l¥ó·s»D:13********************************* *@microsoft.com...
That is because that constructor is not used for passing in the Value of
the
parameter. It is used to pass in the name of the sourceColumn which would
be
used when the Update method of the SqlDataAdapter is called (and assuming
the
command object which holds this parameter is participating in the update
process)

// constructor
public SqlParameter(string parameterName, SqlDbType dbType, int size,
string
sourceColumn)
{

}

you are actually adding in the name of the sourceColumn and NOT the
value...

the constructor accepts the value of the parameter in 2 forms:

// #1
public SqlParameter(string parameterName, object value) { .. }

// #2
public SqlParameter(string parameterName, SqlDbType dbType, int size,
ParameterDirection direction, bool isNullable, byte precision, byte
scale,
string sourceColumn, DataRowVersion sourceVersion, object value) { .. }
Thus:

SqlParameter parameter = new
SqlParameter("@Address",SqlDbType.NVarChar,90);
parameter.Value = txtAddress.Text;

or if you are referring to the actual command object:

command.Parameters.Add("@Address",SqlDbType.NVarCh ar,90).Value =
txtAddress.Text;

Regards,

Brandon
"Jason Huang" wrote:
Hi,

The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is
working for update,
but SqlParameter myPM =new SqlParameter
("@Address",SqlDbType.NVarChar,90,txtAddress.Te xt) is not working.
The Address column is DataType NVarChar 90, no problem.
Any idea?
Jason


Nov 17 '05 #4
typo
====
public void UpdateByParameter(string sql, SqlParameter [] parameters)
{
InternalUpdateByParameter(sql,parameters);
}

public void UpdateByParameter(string sql, MicrosoftSqlParameterCollection
parameters)
{
InternalUpdateByParameter(sql,parameters);
}

private void InternalUpdateByParameter(string sql, ICollection parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(sql,connection))
{
connection.Open();
using (SqlTransaction transaction = connection.BeginTransaction())
{
command.Transaction = transaction;
if (parameters != null)
{
foreach(SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
command.ExecuteNonQuery();
transaction.Commit();
}
}
}
}
and thus,
StringBuilder builder = new StringBuilder();
builder.Append(@"UPDATE ContactAddr SET Serial=@Serial, ");
builder.Append(@"ZipCode =@ZipCode, Address=@Address ");
builder.Append(@"WHERE CustNo=@CustNo AND Serial=@Serial ");
MicrosoftSqlParameterCollection parameters = new
MicrosoftSqlParameterCollection();
// icky though as you are passing a string to be converted to a small
integer
// but I am just showing the example
parameters.Add("@Serial", SqlDbType.SmallInt, 2,txtSerial.Text);
parameters.Add("@ZipCode", SqlDbType.Char, 10,txtAddress.Text);
UpdateByParameter(builder.ToString(),parameters);

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP15.phx.gbl...
Thanks Brandon!
I made up an UpdatByParameter function.
Would you give me some advice to improve my UpdateByParameter function?
strUpdate ="UPDATE ContactAddr SET Serial=@Serial, ";
strUpdate +="ZipCode =@ZipCode, Address=@Address ";
strUpdate +="WHERE CustNo=@CustNo AND Serial=@Serial ";
SqlParameter [] myPM=new SqlParameter[4];
myPM[0] = new SqlParameter("@Serial", SqlDbType.SmallInt, 2);
myPM[1] = new SqlParameter("@ZipCode", SqlDbType.Char, 10);
myPM[2] = new SqlParameter("@Address", SqlDbType.NVarChar, 90);
myPM[3] = new SqlParameter("@CustNo", SqlDbType.NVarChar, 8);
myPM[0].Value=txtSerial.Text;
myPM[1].Value=txtZipCode.Text;
myPM[2].Value=txtAddress.Text;
myPM[3].Value=txtCustNo.Text;
this.UpdatByParameter(strUpdate, myPM, 1);

public void UpdatByParameter(string strSQL, SqlParameter []
GoodParameter, int i)
{
mConn=new SqlConnection(gstrConn);//new connection need to be here first
mConn.Open();
mComm=new SqlCommand(strSQL,mConn);
for (int j=0; j < i; j ++)
{
mComm.Parameters.Add(GoodParameter [j]); //2005.10.19
}
mTrans = mConn.BeginTransaction();
mComm.Transaction=mTrans;
try
{
mComm.ExecuteNonQuery();
mTrans.Commit();
}
catch(SqlException e)
{
mTrans.Rollback();
}
mConn.Close();
}//end UpdateByParameter

"Brandon Driesen" <Brandon Dr*****@discussions.microsoft.com>
¼¶¼g©ó¶l¥ó·s»D:13********************************* *@microsoft.com...
That is because that constructor is not used for passing in the Value of
the
parameter. It is used to pass in the name of the sourceColumn which would
be
used when the Update method of the SqlDataAdapter is called (and assuming
the
command object which holds this parameter is participating in the update
process)

// constructor
public SqlParameter(string parameterName, SqlDbType dbType, int size,
string
sourceColumn)
{

}

you are actually adding in the name of the sourceColumn and NOT the
value...

the constructor accepts the value of the parameter in 2 forms:

// #1
public SqlParameter(string parameterName, object value) { .. }

// #2
public SqlParameter(string parameterName, SqlDbType dbType, int size,
ParameterDirection direction, bool isNullable, byte precision, byte
scale,
string sourceColumn, DataRowVersion sourceVersion, object value) { .. }
Thus:

SqlParameter parameter = new
SqlParameter("@Address",SqlDbType.NVarChar,90);
parameter.Value = txtAddress.Text;

or if you are referring to the actual command object:

command.Parameters.Add("@Address",SqlDbType.NVarCh ar,90).Value =
txtAddress.Text;

Regards,

Brandon
"Jason Huang" wrote:
Hi,

The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is
working for update,
but SqlParameter myPM =new SqlParameter
("@Address",SqlDbType.NVarChar,90,txtAddress.Te xt) is not working.
The Address column is DataType NVarChar 90, no problem.
Any idea?
Jason


Nov 17 '05 #5
Thanks Brandon.
I noticed that you have "using" inside the InternalUpdateByParameter.
How can we have an "using" in a function? Isn't the "using" just for in the
starting of a program?

private void InternalUpdateByParameter(string sql, ICollection parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
Nov 17 '05 #6

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

Similar topics

5
by: Kenneth | last post by:
Can anyone explain me why it is neccesary to include SqlDbType to the SqlParameter. In every example I see, it is done, but no one explaines why. I have for example a date I want to save into my...
3
by: jiangyh | last post by:
hi everyone: SqlParameter class has a property that is DbType looks like following source public DbType DbType { get { // TODO: Add DBParameter.DbType getter implementation return new...
1
by: None | last post by:
Hi, I have to update my table using SqlParameter. Below is my code for update. cmdText = "update docs set Content = @content where DirName='" + DirName.Replace("'","''") + "' and...
4
by: Jason Huang | last post by:
Hi, I want to use the Sqlparameter and SqlDataAdapter to update my data, and the data will be updated based on two TextBoxes txtCustName and txtCustAddress. Thanks for help. Jason
2
by: Stephan | last post by:
Hi, I'm experiencing the problem mentioned in the title above. Briefly, I build a page in which I include a class. This class contains a procedure to add a value to an ArrayList. The value is...
0
by: Elliot M. Rodriguez | last post by:
I implemented a very small, basic data access layer for my web application. It works just fine, except for this one bug. One of my methods returns an abstracted dataset. To accomodate X number of...
3
by: Stacey Levine | last post by:
I have a webservice that has the below procedure. Basically a procedure to called a stored procedure and return the results. When I try to call the webservice from my program I get the error. Both...
6
by: Tim Zych | last post by:
' Declare a new parameter object Dim param() As SqlParameter = New SqlParameter(0) {} ' Set this to null and make it an InputOutput parameter param(0) = New SqlParameter("@Something, DBNull.Value)...
1
by: tsorgi | last post by:
I'm writing a data conversion application to support a system migration. The code is reading formatted text files and loading them into MS SQL 2005 database tables. I wanted to call the existing...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.