Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ive made a asp.net webform containing some listboxes,textboxes, dropdown lists etc. The selection mode for list box is multiple. When I run my code and enter the values in the various boxes and click the go button, the same page is returned with selected items and the data doesn't go into the database.
What to do....Plz help :(
The code is:

C#
private void Postjob(string username,string companyname,string locations,string experiencemin,string experiencemax,string keyskills, string funcArea,string Industry, string postdate, string about, string desc)
    {
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Project3\\App_Data\\ojb1.mdf;Integrated Security=True;User Instance=True");
        String sql = "INSERT INTO db(username,Companyname,Locations,Experiencemin,Experiencemax,Keyskills,Functionarea,Industry,Posted_date,About_Company,Job_Description)" +"VALUES(@userName,@Companyname,@Locations,@Experiencemin,@Experiencemax,@Keyskills,@Functionarea,@Industry,@Posted_date,@About_Company,@Job_Description)";

        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlParameter[] param = new SqlParameter[11];

            param[0] = new SqlParameter("@userName", SqlDbType.VarChar, 50);
            param[1] = new SqlParameter("@Companyname", SqlDbType.VarChar, 50);
            param[2] = new SqlParameter("@Locations", SqlDbType.VarChar, 100);
            param[3] = new SqlParameter("@Experiencemin", SqlDbType.VarChar, 50);
            param[4] = new SqlParameter("@Experiencemax", SqlDbType.VarChar, 50);
            param[5] = new SqlParameter("@Keyskills", SqlDbType.VarChar, 1000);
            param[6] = new SqlParameter("@Functionarea", SqlDbType.VarChar, 50);
            param[7] = new SqlParameter("@Industry", SqlDbType.VarChar, 50);
            param[8] = new SqlParameter("@Posted_date", SqlDbType.VarChar, 50);
            param[9] = new SqlParameter("@About_Company", SqlDbType.VarChar, 1000);
            param[10] = new SqlParameter("@Job_Description", SqlDbType.VarChar, 1000);


            param[0].Value = username;
            param[1].Value = companyname;
            param[2].Value = locations;
            param[3].Value = experiencemin;
            param[4].Value = experiencemax;
            param[5].Value = keyskills;
            param[6].Value = funcArea;
            param[7].Value = Industry;
            param[8].Value = postdate;
            param[9].Value = about;
            param[10].Value = desc;

               for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            
            

        }
        catch (SqlException ex)
        {
            string msg = "Insert Error";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            con.Close();
        }
    }


C#
protected void Submit_Click(object sender, EventArgs e)
   {
       if (CompanyName.Text != "" && ListBoxLoc.SelectedItem.Text != "" && Expmin.Text != "" && Expmax.Text != "" && Keyskill.Text != "" && ListBoxFun.SelectedItem.Text != "" && ListBoxInd.SelectedItem.Text != "" && abtcomp.Text != "" && JobDesc.Text != "")
       {
           Postjob(username.Text, CompanyName.Text, ListBoxLoc.SelectedItem.Text, Expmin.Text, Expmax.Text, Keyskill.Text, ListBoxFun.SelectedItem.Text, ListBoxInd.SelectedItem.Text, postdate.Text, abtcomp.Text, JobDesc.Text);
           Response.Redirect("Home.aspx");
           
       }

C#

Posted
Updated 6-Nov-11 18:41pm
v2

 
Share this answer
 
Did you checked whether the execution entering into the true part of IF condition ?

it looks like button click in triggering only a post back, not a call to database
 
Share this answer
 
v2
Quote:
SqlConnection connectionSql = new SqlConnection("Data Source=(local);Integrated Security=sspi");

SqlCommand commandSql = new SqlCommand();

commandSql.Connection=connectionSql;

commandSql.CommandText="INSERT INTO dbo.Employee (EmployeeName) VALUES (@employeeName)";

commandSql.Parameters.Add("@employeeName");

try

{

connectionSql.Open();

for (int indexCounter = 0; indexCounter < listBoxEmployeeName.Items.Count; indexCounter++)

{

commandSql.Parameters["employeeName"].Value=listBoxEmployeeName.Items[indexCounter].Text;

commandSql.ExecuteNonQuery();

}


connectionSql.Close();

}

catch (Exception exceptionMessage)

{

MessageBox.Show(exceptionMessage.ToString());

}

finally

{
commandSql.Dispose()

connectionSql.Close();

connectionSql.Dispose();

}

MessageBox.Show("Saved to Database!");
 
Share this answer
 
Comments
Member 8667247 22-Mar-12 5:38am    
commandSql.Parameters["employeeName"].Value=listBoxEmployeeName.Items[indexCounter].Text;
in this line we get an error

Error -'object' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
I can not identify any error in the code. Probably there is something else that prevents your code to insert data into your database. I would recommend you to set breakpoint at specific function that you suspect and then debug each line one by one. Check the data of the variable in watch window. I think you may identify your problem. Possible conditions of this type of problem:

**May be your if condition is not working that means program pointer don't go inside if for your given test case. Debug it.

**May be your SQL Connection is not set or busy.

Happy Coding....:-)
 
Share this answer
 

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