HighTechTalks DotNet Forums  

Differant between Sqlcommand object and datasource object

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Differant between Sqlcommand object and datasource object in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
=?Utf-8?B?SHVzYW0=?=
 
Posts: n/a

Default Differant between Sqlcommand object and datasource object - 08-18-2007 , 02:34 AM







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("LocalSqlSe rver").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.ConnectionStrin gs("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


Reply With Quote
  #2  
Old   
Norman Yuan
 
Posts: n/a

Default Re: Differant between Sqlcommand object and datasource object - 08-18-2007 , 10:03 AM






Add "@" to the parameter name:

mydatasource.InsertParameters.Add("@user_Name", TextBox1.Text)
mydatasource.InsertParameters.Add("@user_Password" , TextBox2.Text)
....

"Husam" <Husam (AT) discussions (DOT) microsoft.com> wrote

Quote:
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("LocalSqlSe rver").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.ConnectionStrin gs("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



Reply With Quote
  #3  
Old   
Tim Van Wassenhove
 
Posts: n/a

Default Re: Differant between Sqlcommand object and datasource object - 08-18-2007 , 01:34 PM



On 2007-08-18, Norman Yuan <NoAddress (AT) NoEmail (DOT) fake> wrote:
Quote:
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/>


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.