You are here:

C#/Data not storing in database

Advertisement


Question
QUESTION: using System;
using System.Collections;
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.Data.Sql;
using System.Data.SqlClient;


namespace Gids_Recritment
{
   public partial class _Default : System.Web.UI.Page
   {
       SqlDataAdapter da = new SqlDataAdapter();
       SqlConnection sqlcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Documents\Visual Studio 2010\Projects\Gids\Gids\App_Data\Database1.mdf;Integrated Security=True;User Instance=True");
       protected void Page_Load(object sender, EventArgs e)
       {
         txt_date.Text = DateTime.Today.ToString("dd/MM/yyyy");
       }
       protected void txt_resume_TextChanged(object sender, EventArgs e)
       {

       }
       protected void txt_date_TextChanged(object sender, EventArgs e)
       {
       }
       protected void ddl_cancity_SelectedIndexChanged(object sender, EventArgs e)
       {

       }
       protected void ddl_canstate_SelectedIndexChanged(object sender, EventArgs e)
       {

       }
       protected void Button1_Click(object sender, EventArgs e)
       {
         DateTime Date = new DateTime();
         Date = System.DateTime.Now.Date;

         if (txt_date.Visible)
         {
         if (txt_date.Text != null && txt_date.Text != string.Empty)
         {
         txt_date.Text = DateTime.Today.ToString("dd/MM/yyyy");
         //Date = Convert.ToDateTime(txt_date.Text);
         //Date = Convert.ToDateTime(Date.ToString("dd-MMM-yyyy"));
         }
         }

         DataBinder ds = new DataBinder();
         sqlcon.Open();
         SqlCommand sqlcmd = new SqlCommand("INSERT INTO AddCandidate VALUES ('" + txt_date.Text + "," + txt_rcname.Text + "," + txt_vendor.Text + "," + txt_clname.Text + "," +
   txt_clnumber.Text + "," + txt_canname.Text + "," + ddl_canstate.SelectedItem.Text + "," + ddl_cancity.SelectedItem.Text + "," + txt_canrate.Text + "," + txt_phnumber.Text + "," +
   txt_email.Text + "," + txt_canskills.Text + "," + ddl_visastatus.SelectedItem.Text + "," +
   ddl_availibilty.SelectedItem.Text + "," + txt_canempname.Text + "," + txt_canempsign.Text + "," +
   txt_resume.Text + "')");
         sqlcmd.Parameters.AddWithValue("@Date", txt_date.Text);
         sqlcmd.Parameters.AddWithValue("@RecruiterName", txt_rcname.Text);
         sqlcmd.Parameters.AddWithValue("@Vendor", txt_vendor.Text);
         sqlcmd.Parameters.AddWithValue("@ClientName", txt_clname.Text);
         sqlcmd.Parameters.AddWithValue("@ClientNumber", txt_clnumber.Text);
         sqlcmd.Parameters.AddWithValue("@CanditateName", txt_canname.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateState", ddl_canstate.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateCity", ddl_cancity.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateRate", txt_canrate.Text);
         sqlcmd.Parameters.AddWithValue("@PhoneNumber", txt_phnumber.Text);
         sqlcmd.Parameters.AddWithValue("@Email", txt_email.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateSkills", txt_canskills.Text);
         sqlcmd.Parameters.AddWithValue("@VisaStatus", ddl_visastatus.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@Availability", ddl_availibilty.SelectedItem.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateEmployerName", txt_canempname.Text);
         sqlcmd.Parameters.AddWithValue("@CandidateEmployerSignature", txt_canempsign.Text);
         sqlcmd.Parameters.AddWithValue("@Resume", txt_resume.Text);
         //sqlcmd.ExecuteNonQuery();
         sqlcon.Close();
         if (fu_resume.HasFile)
         {
         fu_resume.SaveAs("D:\\My Documents\\Visual Studio 2010\\Projects\\Resumes\\" + fu_resume.FileName);
         }
         else
         {

         }
       }
       private void VALUES(string p)
       {
         throw new NotImplementedException();
       }
       public string filename { get; set; }
   }
}
if i uncomment  //sqlcmd.ExecuteNonQuery();
it gives error
Invalidoperationquery: connection has not been intialized

and thanks for your last answer it worked

ANSWER: You have to create SqlCommand with an SqlConnection.
Replace that line with the following one and hope it works.


SqlCommand sqlcmd = new SqlCommand("INSERT INTO AddCandidate VALUES ('" + txt_date.Text + "," + txt_rcname.Text + "," + txt_vendor.Text + "," + txt_clname.Text + "," +
  txt_clnumber.Text + "," + txt_canname.Text + "," + ddl_canstate.SelectedItem.Text + "," + ddl_cancity.SelectedItem.Text + "," + txt_canrate.Text + "," + txt_phnumber.Text + "," +
  txt_email.Text + "," + txt_canskills.Text + "," + ddl_visastatus.SelectedItem.Text + "," +
  ddl_availibilty.SelectedItem.Text + "," + txt_canempname.Text + "," + txt_canempsign.Text + "," +
  txt_resume.Text + "')", sqlcon);


---------- FOLLOW-UP ----------

QUESTION: http://www.mediafire.com/?qq9ixe4c34f9qdh
here is my project same error again

Answer
Actually your SqlCommand should be as following:

SqlCommand sqlcmd = new SqlCommand("INSERT INTO AddCandidate VALUES (@Date,@RecruiterName,@Vendor,@ClientName,@ClientNumber,@CanditateName,@CandidateState,@CandidateCity,@CandidateRate,@PhoneNumber,@Email,@CandidateSkills,@VisaStatus,@Availability,@CandidateEmployerName,@CandidateEmployerSignature,@Resume)", sqlcon);
sqlcon.Open();


If this does not work, then you will have to check your connection string.

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Murat Mehmet

Expertise

I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.

Experience

I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Organizations
Was in R & D for 2 years in a popular Turkish technology website: cyber-warrior.com

Education/Credentials
2011 Computer Engineering graduation in University of Trakia in Turkey.

©2016 About.com. All rights reserved.