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

C# SqlParameter SqlDataAdapter

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
Nov 17 '05 #1
4 4316
Could you provide a bit more information ...
the following example show how to update a particular customer's data based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @Data WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:
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

Nov 17 '05 #2
@Data should be @CusData

"Ivan Wong" wrote:
Could you provide a bit more information ...
the following example show how to update a particular customer's data based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @CusData WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:
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

Nov 17 '05 #3
Thanks Ivan.
I am thinking in the "myCommand.Parameters.Add(new SqlParameter(@CusName,
txtCustName.Text);", do we need to specify the SqlDbType for the CusName
column?
Will that make any difference?

Jason

"Ivan Wong" <Iv******@discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D:8C********************************* *@microsoft.com...
@Data should be @CusData

"Ivan Wong" wrote:
Could you provide a bit more information ...
the following example show how to update a particular customer's data
based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @CusData
WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:
> 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
>
>
>

Nov 17 '05 #4
Absolutely, you should specify the sqldbtype for each of the sqlParameter
in the example the sqlDbType of the @CusName is varChar, if the
CustomerName column has a different type then it will throw exception ...

It is always important to specify the type and the precision of the value
in order to avoid sql injection attack.

Hope it helps
Ivan

"Jason Huang" wrote:
Thanks Ivan.
I am thinking in the "myCommand.Parameters.Add(new SqlParameter(@CusName,
txtCustName.Text);", do we need to specify the SqlDbType for the CusName
column?
Will that make any difference?

Jason

"Ivan Wong" <Iv******@discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D:8C*********************** ***********@microsoft.com...
@Data should be @CusData

"Ivan Wong" wrote:
Could you provide a bit more information ...
the following example show how to update a particular customer's data
based on
the value of txtCustName and the value of txtCustAddress

//assume there is a SqlConnection called myConnection
SqlCommand myCommand = new SqlCommand("", myConnection);
myCommand.CommandText = "UPDATE Customers SET CustomerData = @CusData
WHERE
CustomerName = @CusName AND CustomerAddress = @CusAddress";
myCommand.Parameters.Add(new SqlParameter(@CusName, txtCustName.Text);
myCommand.Parameters.Add(new SqlParameter(@CusAddress,
txtCustAddress.Text);
myCommand.Parameters.Add(new SqlParameter(@CusData, txtCustData.Text);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
}

SqlDataAdapter is only used when u update the database from a dataset

cheers,
Ivan

"Jason Huang" wrote:

> 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
>
>
>


Nov 17 '05 #5

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...
1
by: Marcin | last post by:
hi, I would like to ask how to serialize class with members like SqlParameter. I try to use BinaryFormatter. public class Example_Class { public SqlParameter sqlParam; public...
2
by: confused | last post by:
Hi, I want to assign a default value of DBNull.Value to my stored procedure parameter, but also have the ability to overwrite it, so: SqlParameter param = new SqlParameter...
5
by: Daniel Groh | last post by:
What is the best way to access a procedure ? MyCommand.Parameters.Add("@param1",typeof(string)); or SqlParameter myParam = new SqlParameter(); myParam.Add("@param1",typeof(string)); What...
5
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is...
0
by: simon | last post by:
I have collection: Dim myParams As New Collection Then I fill it with parameters. Now, I would like to use it more than 1 time. When I create first dataAdapter, it works:
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)...
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: 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...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.