[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

sqldatasource

Posted on 2008-01-25
7
Medium Priority
?
546 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:Mayank_Agarwal
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20748234
Hi Mayank_Agarwal,

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

jpaulino
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20748245
Also "txtSiteName" shouldn't be the textbox ? If the txtSiteName.Text is empty you will receive that error.

jpaulino
0
 
LVL 1

Author Comment

by:Mayank_Agarwal
ID: 20748253
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 1

Author Comment

by:Mayank_Agarwal
ID: 20748265
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
 
LVL 48

Accepted Solution

by:
Jorge Paulino earned 375 total points
ID: 20748270
Add this

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

Author Closing Comment

by:Mayank_Agarwal
ID: 31425199
Thanks Mate I managed to find the solution, some what using your technique.
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20748273
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question