Solved

Error in inserting record

Posted on 2008-06-15
8
209 Views
Last Modified: 2013-12-17
I have code as listed below. This code is working perfectly if i use MS Access (oleDbConnection) but it giving me error when i convert to use sql server 2005 express edition.
protected void btnAdd_Click(object sender, EventArgs e)
    {
 
        SqlConnection conNwind;
        SqlCommand cmdSelect, cmdSelectID;
        SqlDataReader dtrCustomer;
 
 
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionNWind"].ConnectionString;
        conNwind = new SqlConnection(connStr);
 
 
        conNwind.Open();
 
        string select = "Select * from Customers where CustomerID=@ID";
        cmdSelectID = new SqlCommand(select, conNwind);
 
        cmdSelectID.Parameters.AddWithValue("@ID", txtId.Text);
        dtrCustomer = cmdSelectID.ExecuteReader();
 
 
        if (dtrCustomer.Read())
        {
            Label1.Text = "ID already exist!";
 
        }
 
        else
        {
            string strInsert = "Insert into Customers(CustomerID,Companyname,Contactname,Phone) values(@customerId,@companyname,@contactname,@phone)";
 
            cmdSelect = new SqlCommand(strInsert, conNwind);
 
            cmdSelect.Parameters.AddWithValue("@customerId", txtId.Text);
            cmdSelect.Parameters.AddWithValue("@companyname", txtCompanyName.Text);
            cmdSelect.Parameters.AddWithValue("@contactname", txtContactName.Text);
            cmdSelect.Parameters.AddWithValue("@phone", txtPhone.Text);
 
            int intAdd = cmdSelect.ExecuteNonQuery();
 
            if (intAdd != 0)
                Label1.Text = "Record successfully added into database!";
 
            else
                Label1.Text = "Record not added into database!";
        }
 
        dtrCustomer.Close();
        conNwind.Close();
 
    }

Open in new window

0
Comment
Question by:nisha_
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 21791138
Where do you get an error and what error do you get when you change it to sql server 2005 express edition?
0
 

Author Comment

by:nisha_
ID: 21791143
Error message : There is already an open DataReader associated with this Command which must be closed first.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 21791219
Where do you get that error ? Can you try this.
protected void btnAdd_Click(object sender, EventArgs e)
    {
 
        SqlConnection conNwind;
        SqlCommand cmdSelect, cmdSelectID;
        SqlDataReader dtrCustomer;
 
 
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionNWind"].ConnectionString;
        conNwind = new SqlConnection(connStr);
 
 
        conNwind.Open();
 
        string select = "Select * from Customers where CustomerID=@ID";
        cmdSelectID = new SqlCommand(select, conNwind);
 
        cmdSelectID.Parameters.AddWithValue("@ID", txtId.Text);
        dtrCustomer = cmdSelectID.ExecuteReader();
 
 
        if (dtrCustomer.Read())
        {
            Label1.Text = "ID already exist!";
            dtrCustomer.Close();
        }
 
        else
        {
            dtrCustomer.Close();
            string strInsert = "Insert into Customers(CustomerID,Companyname,Contactname,Phone) values(@customerId,@companyname,@contactname,@phone)";
 
            cmdSelect = new SqlCommand(strInsert, conNwind);
 
            cmdSelect.Parameters.AddWithValue("@customerId", txtId.Text);
            cmdSelect.Parameters.AddWithValue("@companyname", txtCompanyName.Text);
            cmdSelect.Parameters.AddWithValue("@contactname", txtContactName.Text);
            cmdSelect.Parameters.AddWithValue("@phone", txtPhone.Text);
 
            int intAdd = cmdSelect.ExecuteNonQuery();
 
            if (intAdd != 0)
                Label1.Text = "Record successfully added into database!";
 
            else
                Label1.Text = "Record not added into database!";
        }
         
        conNwind.Close();
 
    }

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 7

Expert Comment

by:AUmidh
ID: 21791221
make a try of the following
add this statement dtrCustomer.Close();
before the following.
string strInsert = "Insert into Customers(CustomerID,Companyname,Contactname,Phone) values(@customerId,@companyname,@contactname,@phone)";

I am not sure about this but may be.


0
 
LVL 19

Expert Comment

by:elimesika
ID: 21791226
Try
     SqlConnection conNwind;
        SqlCommand cmdSelect, cmdSelectID;
        SqlDataReader dtrCustomer;
 
 
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionNWind"].ConnectionString;
        conNwind = new SqlConnection(connStr);
 
 
        conNwind.Open();
 
        string select = "Select * from Customers where CustomerID=@ID";
        cmdSelectID = new SqlCommand(select, conNwind);
 
        cmdSelectID.Parameters.AddWithValue("@ID", txtId.Text);
        dtrCustomer = cmdSelectID.ExecuteReader();
 
 
        if (dtrCustomer.Read())
        {
            Label1.Text = "ID already exist!";
            dtrCustomer.Close();
            conNwind.Close();
 	    return;
        }
 
        else
        {
            dtrCustomer.Close();
            string strInsert = "Insert into Customers(CustomerID,Companyname,Contactname,Phone) values(@customerId,@companyname,@contactname,@phone)";
 
            cmdSelect = new SqlCommand(strInsert, conNwind);
 
            cmdSelect.Parameters.AddWithValue("@customerId", txtId.Text);
            cmdSelect.Parameters.AddWithValue("@companyname", txtCompanyName.Text);
            cmdSelect.Parameters.AddWithValue("@contactname", txtContactName.Text);
            cmdSelect.Parameters.AddWithValue("@phone", txtPhone.Text);
 
            int intAdd = cmdSelect.ExecuteNonQuery();
 
            if (intAdd != 0)
                Label1.Text = "Record successfully added into database!";
 
            else
                Label1.Text = "Record not added into database!";
                
            conNwind.Close();
        }
 

Open in new window

0
 

Author Comment

by:nisha_
ID: 21791255
putting dtrCustomer.Read() before insert statement make the program work. But why i should do like that since my insert statement not using data reader.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21791298
A datareader opens a transaction. As long as it's not commited (or closed), the connection will be exclusive used by your datareader.
0
 

Author Comment

by:nisha_
ID: 21791307
Then how it can work in MS Access even i closed the datareader after insert statement?
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net WebRequest Code from JSON curl 7 50
JSON Deserialize issue 6 26
VB.NET Textbox input validation 4 40
how to just get time from a date 6 33
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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