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


here is my project if you can downlad and check that would be great hel to me  http://www.mediafire.com/?qq9ixe4c34f9qdh

ANSWER: I have downloaded you project but it has no Default.aspx and also has no database file. I have answered you in your previous question.
Please replace these whole lines:

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 + "')");

with

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();

and it should work.

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

QUESTION: actually i tried that and its not working

Answer
Then your connection string may be broken too. Ensure that the database (mdf) file is inside your App_Data directory.

Additionally, check if you have Sql Server Express installed

See forums.asp.net for more information.

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.