473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Same select query in same button event

I am trying to insert details in the database using ASP.NET code. If any one of the data or field value is already existing,it will show an alert message 'already existing'.

I will show the code:
Expand|Select|Wrap|Line Numbers
  1.  protected void btnSave_Click(object sender, EventArgs e)
  2.         { 
  3.  
  4.             lblMessage.Text = string.Empty;
  5.             if (txtFName.Text == String.Empty || txtLName.Text == String.Empty || txtAccount.Text == String.Empty || txtAddress.Text == String.Empty || txtJoiningDate.Text == String.Empty || txtMailID.Text == String.Empty || txtPassword.Text == String.Empty)
  6.             {
  7.                 lblMessage.Text = "Please Enter Valid Values";
  8.             }
  9.  
  10.  
  11.             else
  12.             {
  13.  
  14.                 try
  15.                 {
  16.                     int lastInserted=0;
  17.                     DataSet ds = new DataSet();
  18.                     SqlConnection conn = new SqlConnection(connstring);
  19.                         conn.Open();
  20.                         SqlCommand Cmd = new SqlCommand("EmployeeDatabase.dbo.insertEmployeeDetails", conn);
  21.                         Cmd.CommandType = CommandType.StoredProcedure;
  22.                         Cmd.Parameters.Add("emp_id", SqlDbType.BigInt);
  23.                         //Cmd.Parameters["emp_id"].Value = txtid.Text;
  24.                         Cmd.Parameters["emp_id"].Value = txtid.Text;
  25.                         Cmd.Parameters.Add("empfirstname", SqlDbType.VarChar);
  26.                         Cmd.Parameters["empfirstname"].Value = txtFName.Text;
  27.                         Cmd.Parameters.Add("emplastname", SqlDbType.VarChar);
  28.                         Cmd.Parameters["emplastname"].Value = txtLName.Text;
  29.                         Cmd.Parameters.Add("accnumber", SqlDbType.VarChar);
  30.                         Cmd.Parameters["accnumber"].Value = Convert.ToInt32(txtAccount.Text);
  31.                         Cmd.Parameters.Add("joiningdate", SqlDbType.DateTime);
  32.                         Cmd.Parameters["joiningdate"].Value = txtJoiningDate.Text;
  33.                         Cmd.Parameters.Add("address", SqlDbType.VarChar);
  34.                         Cmd.Parameters["address"].Value = txtAddress.Text;
  35.                         Cmd.Parameters.Add("typeofuser", SqlDbType.VarChar);
  36.                         Cmd.Parameters["typeofuser"].Value = ddlUser.SelectedItem.ToString();
  37.                         Cmd.Parameters.Add("emailid", SqlDbType.VarChar);
  38.                         Cmd.Parameters["emailid"].Value = txtMailID.Text;
  39.                         Cmd.Parameters.Add("active", SqlDbType.Bit);
  40.                         Cmd.Parameters["active"].Value = Convert.ToInt32(ddlActive.SelectedValue);
  41.                         Cmd.Parameters.Add("password", SqlDbType.VarChar);
  42.                         Cmd.Parameters["password"].Value = txtPassword.Text;
  43.  
  44.                         //int rows = Cmd.ExecuteNonQuery();
  45.                         SqlDataReader reader = Cmd.ExecuteReader();
  46.                         while (reader.Read())
  47.                         {
  48.  
  49.  
  50.  
  51.                            Cmd = new SqlCommand("select * from EmployeeDatabase.dbo.Employee_Table where Emp_ID="+lastInserted, conn);
  52.  
  53.                             //conn.Open();
  54.                             //Cmd.ExecuteNonQuery();
  55.                             //lastInserted = Int32.Parse(Cmd.ExecuteScalar().ToString());
  56.                             lblMessage.Text = "Succesfully added";
  57.                         }
  58.                         reader.Dispose();
  59.                         Cmd.Dispose();
  60.                         conn.Close();
  61.                         grdEmployee.DataSource = ds;
  62.                         grdEmployee.DataBind();
  63.  
  64.                         conn.Open();
  65.                         //Cmd = new SqlCommand("select * from EmployeeDatabase.dbo.Employee_Table where Emp_ID="+lastInserted, conn);
  66.                       Cmd = new SqlCommand("SELECT Email_ID FROM EmployeeDatabase.dbo.Employee_Table WHERE  Email_ID='" + txtMailID.Text + "'", conn);
  67.  
  68.                          reader = Cmd.ExecuteReader();
  69.  
  70.                         if (reader != null && reader.HasRows)
  71.                         {
  72.                             lblMessage.Text = "Emailid already exist";
  73.                         }
  74.                         else
  75.                         {
  76.                             lblMessage.Text = "successfully added";
  77.                         }
  78.  
  79.                         reader.Dispose();
  80.                         SqlDataAdapter da = new SqlDataAdapter(Cmd);
  81.                         da.Fill(ds,"Employee_Table");
  82.                         da.Dispose();
  83.                         Cmd.Dispose();
  84.                         grdEmployee.DataSource = ds.Tables["Employee_Table"];
  85.                         grdEmployee.DataBind();
  86.                         conn.Close();
  87. }
But it is showing an error the ilistsource does not contain any data sources.

What does it mean?
Sep 20 '11 #1
1 1543
Rabbit
12,516 Expert Mod 8TB
I don't know what insertEmployeeDetails does but it sounds like it inserts a new record. You're probably hitting up against a unique index conflict.
Sep 20 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: yankee | last post by:
Hi all, I have the following HTML code: <form> <table border=1 cellspacing=0 cellpadding=3> <tr> <td>Select a Document:</td> <td><SELECT name="type" id="type"> <OPTION
9
by: MLH | last post by:
I have a table (tblCorrespondence) holding records with fields like , , , , , , , etc... About a dozen 's are defined and I often use queries to extract records of a given . That's pretty easy....
2
by: Sam Miller | last post by:
Hi, I have a button event that won't fire. I left it on Friday and it worked fine. I came back in on Monday and it won't fire. I tried putting another button and just putting a...
4
by: Bishop | last post by:
I can't get my button event to fire inside a table. the button outside of the table works. Both make a postback. Any help appreciated. My code below. (Add button to page)
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
6
by: DeanL | last post by:
Hi All, How do you run a SELECT query from code? I know that DoCmd.RunSQL only works with action or DDF queries but I need to run a query from VBA and add variables into the query as defined by...
2
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and...
1
by: =?Utf-8?B?U3Bhcmt5IDcxNzc=?= | last post by:
Hi, I am facing one deployment issue, I have 3 aspx pages one for Login, other for search request form and the third for displaying the results. After it was working fine locally I deployed it on...
1
by: runway27 | last post by:
hi i have registration form where user selects from a drop down to select their area code apart from filling other details in the form. in the next page which is a confirmation page i would like...
2
jlm699
by: jlm699 | last post by:
I'm at the end of my wits here and can't possibly see what I'm doing wrong. I tried doing a "COPY (query) TO '/var/tmp/filename.csv' WITH CSV", which gave me an error. So I tried it w/o the WITH...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.