Differant between Sqlcommand object and datasource object

G

Guest

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
 
N

Norman Yuan

Add "@" to the parameter name:

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

Tim Van Wassenhove

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top