Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

I am trying to add data to my SQL Server database using LINQ and Colections.

In the front end, I am using a List Box Control, in which it shows the data as added, but in the backend, the database is not updated.

Below is the source code, There is no error or exception prompted.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<test1> tests = GetNames();
        var q = from c in tests
                
                select c;
        foreach (var i in q)
        {
            ListBox1.Items.Add(i.FirstName + i.LastName);
        }

        
        

    }

     List<test1> GetNames()
    {
        List<test1> t = new List<test1>();
        t.Add(new test1("Albert", "Einstein"));
        return t;
     
    }
}

</test1></test1></test1></test1>


The above source code was for the Default.aspx.cs file.

I have another *.cs file. Below is the Code.
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public class test1
{
    private string first_name;
    public string FirstName
    {
        get { return first_name; }
        set { first_name = value; }
    }
    private string last_name;
    public string LastName
    {
        get { return last_name; }
        set { last_name = value; }
    }

	public test1(string FirstName,string LastName)
	{
        first_name = FirstName;
        last_name = LastName;
    
	}
}



There are only 2 fields in the database First Name and Last Name.

Kindly Assist
Many Thanks and Regards
Posted

Hi there,

It looks as though there is no code in what you've posted to actually write to the database. You're not using LINQ to SQL, you're using LINQ to objects.

Might I suggest a read[^]?

It will be worth going through start-to-finish to get an understanding about what is required to work with LINQ as it relates to creating data in the database.

The key thing will be that with the help of a DBML file, you'll not need to create classes from scratch (there will be partial classes created for you) and updating the database - including writing new data to it - is as simple as calling a method named SubmitChanges() on your DataContext.

Cheers.
 
Share this answer
 
Comments
Mukund Kallapur 17-Aug-10 12:04pm    
Thank you very much for the answer. But I also tried to add items in the database as mentioned by you. Regarding that also I had posted query in this site, but I did not get an answer which could resolve my problem.

Again I will put only the part where I am getting the error, " string connectionString = ConfigurationManager.ConnectionStrings["Data Source=M-EDE94949520E4''SQLEXPRESS;Initial Catalog=test;Integrated Security=True"].ToString();".

When I try to enter the names through the Text Boxes in my page, I get the error for NullReferenceException.

The error says "Use "new" keyword to create an object instance" pointing to this line.

I hope some assistance from you !!
string connectionString = ConfigurationManager.ConnectionStrings["Data Source=M-EDE94949520E4''SQLEXPRESS;Initial Catalog=test;Integrated Security=True"].ToString();


This is not likely at all a valid entry in your web.config. You need to find the name of your connection string and replace the text between the [] to set the correct key.

Cheers.
 
Share this answer
 
Comments
Mukund Kallapur 17-Aug-10 12:41pm    
Hey...this is not an entry in the web.config file. This is the code which I am using in the program
TheyCallMeMrJames 17-Aug-10 12:44pm    
regardless, you'll need to fix the string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900