|
|
|
start date: Fri, 17 Aug 2007 23:34:06 -0700,
posted on: microsoft.public.dotnet.framework.adonet
back
| Thread Index |
|
1
Husam
|
|
2
Norman Yuan
|
|
3
Tim Van Wassenhove am
|
Differant between Sqlcommand object and datasource object
Hi EveryBody:
Whey when I use the following code trying to inser my data to database it
did not work:
Dim mydatasource As New SqlDataSource
mydatasource.ConnectionString =
ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString
mydatasource.InsertCommandType = SqlDataSourceCommandType.Text
mydatasource.InsertCommand = "INSERT INTO
husam_Tab(user_Name,user_Password,user_Email,user_Squestion,user_Sanswer)
Values(@user_Name,@user_Password,@user_Email,@user_Squestion,@user_Sanswer)"
mydatasource.InsertParameters.Add("user_Name", TextBox1.Text)
mydatasource.InsertParameters.Add("user_Password", TextBox2.Text)
mydatasource.InsertParameters.Add("user_Email", TextBox4.Text)
mydatasource.InsertParameters.Add("user_Squestion", TextBox5.Text)
mydatasource.InsertParameters.Add("user_Sanswer", TextBox6.Text)
While Whe I use the foolowing code to insert the same data to my database
Its Work:
Dim cmd As SqlCommand
Dim scon As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
Dim sql As String
scon.Open()
sql = "INSERT INTO
husam_Tab(user_Name,user_Password,user_Email,user_Squestion,user_Sanswer)" +
"Values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox4.Text &
"','" & TextBox5.Text & "','" & TextBox6.Text & "')"
cmd = New SqlCommand(sql, scon)
cmd.ExecuteNonQuery()
scon.Close()
Note:
I ma using SQL server 2005 Not Express edition so my database file it is not
attached to my solution.
Any help will be completlly appreciated
regard's
Husam
Date:Fri, 17 Aug 2007 23:34:06 -0700
Author:
|
Re: Differant between Sqlcommand object and datasource object
Add "@" to the parameter name:
mydatasource.InsertParameters.Add("@user_Name", TextBox1.Text)
mydatasource.InsertParameters.Add("@user_Password", TextBox2.Text)
....
"Husam" wrote in message
news:2078C3BE-7EC6-4034-9B97-6893F9AD8316@microsoft.com...
>
> Hi EveryBody:
>
> Whey when I use the following code trying to inser my data to database it
> did not work:
>
> Dim mydatasource As New SqlDataSource
> mydatasource.ConnectionString =
> ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString
> mydatasource.InsertCommandType = SqlDataSourceCommandType.Text
> mydatasource.InsertCommand = "INSERT INTO
> husam_Tab(user_Name,user_Password,user_Email,user_Squestion,user_Sanswer)
> Values(@user_Name,@user_Password,@user_Email,@user_Squestion,@user_Sanswer)"
>
> mydatasource.InsertParameters.Add("user_Name", TextBox1.Text)
> mydatasource.InsertParameters.Add("user_Password", TextBox2.Text)
> mydatasource.InsertParameters.Add("user_Email", TextBox4.Text)
> mydatasource.InsertParameters.Add("user_Squestion", TextBox5.Text)
> mydatasource.InsertParameters.Add("user_Sanswer", TextBox6.Text)
>
>
> While Whe I use the foolowing code to insert the same data to my database
> Its Work:
>
> Dim cmd As SqlCommand
> Dim scon As SqlConnection = New
> SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
> Dim sql As String
> scon.Open()
> sql = "INSERT INTO
> husam_Tab(user_Name,user_Password,user_Email,user_Squestion,user_Sanswer)"
> +
> "Values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox4.Text
> &
> "','" & TextBox5.Text & "','" & TextBox6.Text & "')"
> cmd = New SqlCommand(sql, scon)
> cmd.ExecuteNonQuery()
> scon.Close()
>
> Note:
>
> I ma using SQL server 2005 Not Express edition so my database file it is
> not
> attached to my solution.
>
> Any help will be completlly appreciated
>
> regard's
>
> Husam
>
Date:Sat, 18 Aug 2007 07:03:44 -0700
Author:
|
Re: Differant between Sqlcommand object and datasource object
On 2007-08-18, Norman Yuan wrote:
> Add "@" to the parameter name:
>
> mydatasource.InsertParameters.Add("@user_Name", TextBox1.Text)
> mydatasource.InsertParameters.Add("@user_Password", TextBox2.Text)
> ...
Notice that his SqlDataSource does not have public methods to perform
the queries.. (ExecuteInsert is protected) which made me presume that
the class probably only should be used by the asp.net framework
itself...
The other remark is that the second parameter of the Add method is only
a default value...
--
Kind regards,
Tim Van Wassenhove <url:http://www.timvw.be/>
Date:Sat, 18 Aug 2007 10:34:58 -0700
Author:
|
|
|