3
Answers

data insertion issue

romasha ali

romasha ali

11y
1.2k
1
sir, i m trying this code but it is not doing anything no data is inserting into table and label is not changing its text, what to do now? 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class course : System.Web.UI.Page
{
//{   // string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

////     protected void Page_Load(object sender, EventArgs e)
////  {
//// con.Open();
////  }
////  protected void Button1_Click(object sender, EventArgs e)
//// {
//// //SqlConnection con = new SqlConnection(connStr);
//// SqlCommand cmd = new SqlCommand("insert into profile values('" + name.Text+ "','" + fname.Text+ "','" + ep.Text+ "','" + cls.Text+ "','" + semester.Text+ "')", con);

//////con.Open();
//// cmd.ExecuteNonQuery();
//////cm.ExecuteNonQuery();
////// con.Close();
    
//    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//    protected void Page_Load(object sender, EventArgs e)
//    {
//        con.Open();
//    }
//    protected void Button1_Click(object sender, EventArgs e)
//    {
//        //SqlCommand cmd = new SqlCommand("insert into profile(studentname,fathername,ep1,class,semester,courseId) values('" +TextBox1.Text + "','" +TextBox2.Text+ "', '" +TextBox3.Text+ "','"+s1+"')", con);
//        //cmd.ExecuteNonQuery();
//        //con.Close();
//        //Label1.Visible = true;
//        //Label1.Text = "Registered Successfully";
//        //TextBox1.Text = "";
//        //TextBox2.Text = "";
//        //TextBox3. Text = "";
        

//string s1 = string.Empty; 
//foreach (ListItem item in this.CheckBoxList1.Items)
//{ if (item.Selected)
// { s1 = item.ToString();
// SqlCommand com = new SqlCommand("insert into profile(studentname,fathername,ep1,class,semester,courseId) values('" + TextBox1.Text + "','" + TextBox2.Text + "', '" + TextBox3.Text + "','" + s1 + "')", con); 
//    com.CommandType = System.Data.CommandType.Text;
//    com.ExecuteNonQuery();
////com.ExecuteNonQuery();

//} }
//Response.Write("Inserted Successfully"); } } 

    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        string s1 = string.Empty;
        foreach (ListItem item in this.CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                s1 = item.ToString();
                SqlCommand cmd = new SqlCommand(@"INSERT INTO PROFILE(studentname,fathername,ep1,class,semester,courseId) VALUES (@STUDNAME,@PARNTNAME,@EP1NAME,@CLASS,@SEMESTER,@COURSEID)", conn);

                cmd.Parameters.AddWithValue("@STUDNAME", TextBox1.Text);
                cmd.Parameters.AddWithValue("@PARNTNAME", TextBox2.Text);
                cmd.Parameters.AddWithValue("@EP1NAME", TextBox3.Text);
                cmd.Parameters.AddWithValue("@CLASS", TextBox4.Text);
                cmd.Parameters.AddWithValue("@SEMESTER", TextBox5.Text);
                cmd.Parameters.AddWithValue("@COURSEID", s1);
                conn.Open();

                Int32 rowsInserted = cmd.ExecuteNonQuery();
                conn.Close();

                if (rowsInserted > 0)
                {
                    Label1.Visible = true;
                    Label1.Text = "Registered Successful";
                    TextBox1.Text = string.Empty;
                    TextBox2.Text = string.Empty;
                    TextBox3.Text = string.Empty;
                    TextBox4.Text = string.Empty;
                    TextBox5.Text = string.Empty;

                }
                else
                {
                    Label1.Visible = true;
                    Label1.Text = "Registration Failed";
                }
            }
        }
    }
            
        
        private void Form1_Load(object sender, EventArgs e)
        {
            Label1.Visible = false;
        }

}

Answers (3)