Differant between Sqlcommand object and datasource object

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Add "@" to the parameter name:

mydatasource.InsertParameters.Add("@user_Name", TextBox1.Text)
mydatasource.InsertParameters.Add("@user_Password", TextBox2.Text)
....
 
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...
 
Back
Top