Hi all
Complete newby trying to understand something
I have made my own form with some fields and then dragged a SqlDataSource
onto the page and made an Insert to go with the SqlDataSource.
I have a button which calls this
Protected Sub InsertTextBox_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles InsertTextBox.Click
Me.SqlDataSource1.InsertParameters.Add("@CompanyID ",
Me.CompanyIDTextBox.Text)
Me.SqlDataSource1.InsertParameters.Add("@ContactNa me",
Me.ContactNameTextBox.Text)
Me.SqlDataSource1.InsertParameters.Add("@CompanyNa me",
Me.CompanyNameTextBox.Text)
Me.SqlDataSource1.InsertParameters.Add("@Postcode" , Me.PostCodeTextBox.Text)
Try
Me.SqlDataSource1.Insert()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
I have checked parameter names etc put it wont put the values to the
parameters. Obviously the .Add is wrong, what should be using?
Cheers 8 14075
MC wrote: Me.SqlDataSource1.InsertParameters.Add("@Postcode" , Me.PostCodeTextBox.Text)
I have checked parameter names etc put it wont put the values to the parameters. Obviously the .Add is wrong, what should be using?
Add is correct, but it doesn't want to tell you it needs to know the data
type (e.g. SqlDbType.NVarChar) and, for strings, the length.
Andrew
Thanks Andrew,
So whats the syntax?
MC
"Andrew Morton" <ak*@in-press.co.uk.invalid> wrote in message
news:eI**************@TK2MSFTNGP09.phx.gbl... MC wrote: Me.SqlDataSource1.InsertParameters.Add("@Postcode" , Me.PostCodeTextBox.Text)
I have checked parameter names etc put it wont put the values to the parameters. Obviously the .Add is wrong, what should be using?
Add is correct, but it doesn't want to tell you it needs to know the data type (e.g. SqlDbType.NVarChar) and, for strings, the length.
Andrew
Depending on what your insert command is called, if you used a
SqlCommand object then it will go like this...
SqlCommand1.Parameters("@CompanyName").Value = CompanyNameTextBox.Text
I must be doing something totally wrong. ADO is confussing me but I am
getting there.
OK and using V2 .net. I could have used a formview but choose not to cause
it does some validation on fields as I go and it was to confusing using the
formview.
So I made my own form and I get the validations working fine.
I hit insert button i created and then with SqlDataSource1 I dragged on to
the webpage (with Insert Command in toe) i created the following procedure.
Protected Sub InsertTextBox_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles InsertTextBox.Click
Me.SqlDataSource1.InsertParameters.Add("@CompanyID ", Me.CompIDTextBox.Text)
Me.SqlDataSource1.InsertParameters.Add("@ContactNa me",
Me.ContactNameTextBox.Text)
Me.SqlDataSource1.InsertParameters.Add("@CompanyNa me",
Me.CompanyNameTextBox.Text)
Me.SqlDataSource1.InsertParameters.Add("@Postcode" , Me.PostCodeTextBox.Text)
Try
Me.SqlDataSource1.Insert()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Which as you know I listed earlier. The Insert Command is as shown below:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:connEducare %>"
DeleteCommand="DELETE FROM [Companies] WHERE [ID] = @original_ID"
InsertCommand="INSERT INTO [Companies] ([CompanyID], [ContactName],
[TelNumber], [FaxNumber], [email], [CompanyName], [Address], [Postcode],
[Username], [Password]) VALUES (@CompanyID, @ContactName, @TelNumber,
@FaxNumber, @Email, @CompanyName, @Address, @Postcode, @Username,
@Password)"
SelectCommand="SELECT * FROM [Companies]" UpdateCommand="UPDATE [Companies]
SET [CompanyID] = @CompanyID, [ContactName] = @ContactName, [TelNumber] =
@TelNumber, [FaxNumber] = @FaxNumber, [email] = @Email, [CompanyName] =
@CompanyName, [Address] = @Address, [Postcode] = @Postcode, [Username] =
@Username, [Password] = @Password WHERE [ID] = @original_ID">
<DeleteParameters>
<asp:Parameter Name="original_ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyID" Type="Int32" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="TelNumber" Type="String" />
<asp:Parameter Name="FaxNumber" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Postcode" Type="String" />
<asp:Parameter Name="Username" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="original_ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CompanyID" Type="Int32" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="TelNumber" Type="String" />
<asp:Parameter Name="FaxNumber" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Postcode" Type="String" />
<asp:Parameter Name="Username" Type="String" />
<asp:Parameter Name="Password" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
And then I get the following error:
Cannot insert the value NULL into column 'CompanyID', table
'Educare.dbo.Companies'; column does not allow nulls. INSERT fails. The
statement has been terminated.
So confused
First try changing this line to...
Me.SqlDataSource1.InsertParameters.Add("@CompanyID ", 32)
Or some other Integer that doesnt violate the database constraints.
MC wrote: Thanks Andrew,
So whats the syntax?
I do it the long way round, e.g:-
Dim sqlCmd As New SqlCommand("AddBasket", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Char, 36)
sqlParam.Value = myUserID
sqlCmd.Parameters.Add(sqlParam)
sqlParam = New SqlParameter("@BasketName", SqlDbType.NVarChar, 24)
sqlParam.Value = basketName
sqlCmd.Parameters.Add(sqlParam)
' etc.
Andrew
Great example Andrew,
I also use the same method you do. IMHO, I think it is a little
cleaner than the way VS adds things.
MC,
It is not so important what you use, if you use the parameters only once,
than you can use your method.
If you will use them more by instance as you do, than at the second button
click you are in trouble, because it is a collection, so you have than 8
etc. parameters while 4 are expected.
Therefore the methode from Andrew has a little bit to be changed..
Form Load event.
Dim sqlCmd As New SqlCommand("AddBasket", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Char, 36)
sqlCmd.Parameters.Add(sqlParam)
sqlParam = New SqlParameter("@BasketName", SqlDbType.NVarChar, 24)
sqlCmd.Parameters.Add(sqlParam)
Button Event
sqlCmd.Parameters("@UserID").Value = myUserID
sqlCmd.Parameters("@BasketName").Value = basketName
I hope this gives an idea.
Cor
<cb****@duclaw.com> schreef in bericht
news:11**********************@g43g2000cwa.googlegr oups.com... Great example Andrew, I also use the same method you do. IMHO, I think it is a little cleaner than the way VS adds things. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fasil |
last post by:
Hi there
I have a really anoying question, which is probably VS 2005, framework
2.0 specific.
Usually when I connect a GridView or a DetailsView to a method in my
business class for example...
|
by: VB Programmer |
last post by:
I have an array list that is filled with ProductId's. I wanted to traverse
thru the arraylist and use these product as parameters in a SqlDataSource.
It seems to always stay on the 1st element in...
|
by: mahajanvit |
last post by:
Hi one and all
I got this problem during my project. So in order to solve this I made
a very small application. I am trying to insert using SP and
sqldatasource control. I know that while using...
|
by: raghav |
last post by:
Hi
I am working on ASP.NET 2.0. I am developing a website using Wizard
control. Based on number of steps added, next, previous, finish buttons
generate automatically. After running the...
|
by: YisMan |
last post by:
Hi Everybody,
I'd Appreciate anybody who can give me a lead here.
I'm trying to run an "Insert" command from my ASP page. The command takes as
values some on-page textboxes as well some other...
|
by: PinkBishop |
last post by:
I am using VS 2005 with a formview control trying to insert a record
to my access db. The data is submitted to the main table no problem,
but I need to carry the catID to the bridge table...
|
by: Dököll |
last post by:
ASP.NET and SQL Server 2005 Database interaction includes:
Language: ASP.NET
Connectivity: SQL Server 2005
Foreword: This database connectivity is nothing of genius, it is a simple...
|
by: Bobby Edward |
last post by:
Using Access db with VS2008 (ASP.NET/VB.NET)....
On the INSERT command I get this error: System.Data.OleDb.OleDbException:
Data type mismatch in criteria expression.
I haven't found a...
|
by: klaul |
last post by:
Hi there. I'm having an issue with autopostback, in that it doesn't seem to be working!
The webform I am using pulls the data from an AccessDataSource to populate the gridview, which has Edit and...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |