sqldatasource

Hi Guys

I am trying to insert a row in tot he table. the code is given below. I have create a sqldatasource using the asp.net wizard. when i try to insert the row i get the error in the * line as below.

Thanks in advance


INSERT INTO [Sites] ([SiteName], [SiteID]) VALUES (@SiteName, @SiteID)
....
            <InsertParameters>
                <asp:Parameter Name="SiteName" Type="String" />
                <asp:Parameter Name="SiteID" Type="Int32" />
            </InsertParameters>
......
 
    Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click
 
        dsGetSiteNames.InsertParameters.Add("@SiteID", 3)
        dsGetSiteNames.InsertParameters.Add("@SiteName", "txtSiteName")
        dsGetSiteNames.Insert()*******
    End Sub
Cannot insert the value NULL into column 'SiteName', table 'ECC_Web_Site.dbo.Sites'; column does not allow nulls. INSERT fails.
The statement has been terminated

Open in new window

LVL 1
Mayank_AgarwalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
Hi Mayank_Agarwal,

You only have those two fields on the table (SiteName and SiteID) ?

jpaulino
0
Jorge PaulinoIT Pro/DeveloperCommented:
Also "txtSiteName" shouldn't be the textbox ? If the txtSiteName.Text is empty you will receive that error.

jpaulino
0
Mayank_AgarwalAuthor Commented:
HI

I only have these tow fields in the table,. i have tried to put txtSiteName.text as well but that did'nt work, so i tried to hardcode it to test it. I am also trying

       Using myConn As New SqlConnection(connStr)
            Dim sql As String = "INSERT INTO [Sites] ([SiteName], [SiteID]) VALUES (@SiteName, @SiteID)"
            Dim myCommand As New SqlCommand(sql, myConn)
            'Dim myDataSet As New DataSet
            myCommand.Parameters.AddWithValue("@siteID", 3)
            myCommand.Parameters.AddWithValue("@SiteName", txtSiteName.Text)
            Dim myAdapter As New SqlDataAdapter(myCommand)
            myAdapter.InsertCommand = myCommand
            myConn.Close()
        End Using
which doesnt work either, this doesnt give my any error but the row is not inserted
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Mayank_AgarwalAuthor Commented:
This seems to be working, i am sure there is an easier way to do this.

        Using myConn As New SqlConnection(connStr)
            Dim sql As String = "INSERT INTO [Sites] ([SiteName], [SiteID]) VALUES (@SiteName, @SiteID)"
            Dim myCommand As New SqlCommand(sql, myConn)
            'Dim myDataSet As New DataSet
            myCommand.Parameters.AddWithValue("@siteID", 3)
            myCommand.Parameters.AddWithValue("@SiteName", txtSiteName.Text)
            myCommand.Connection.Open()
            myCommand.ExecuteNonQuery()
            myCommand.Connection.Close()
            myConn.Close()
        End Using
0
Jorge PaulinoIT Pro/DeveloperCommented:
Add this

myAdapter.InsertCommand = myCommand
Dim x As Integer = myCommand.ExecuteNonQuery()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank_AgarwalAuthor Commented:
Thanks Mate I managed to find the solution, some what using your technique.
0
Jorge PaulinoIT Pro/DeveloperCommented:
You already have post it.  You don't need to open the connection again. If you define x as integer you can check if there is any affected record.

If x > 0 Then
 ' x records inserted
Else
 ' 0 records inserted
End If
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.