0

I have this code to enter data of checked boxes to be entered in other table of access database. I have taken template field and added a text box in Item Template but how to enter the text box data in a database on a button click.

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Button1.Attributes.Add("onclick", "return confirm('Are you sure you want to select this units?');");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = string.Empty;
        string tid = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
        {
            TextBox tb = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1"); //find the TextBox
            if (tb != null)
            {
                if (tb.Enabled)
                {
                    tid += GridView1.Rows[i].Cells[1].Text;
                    tid += ",";
                }
            }
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
            if (cb != null)
            {
                if (cb.Checked)
                {
                    id += GridView1.Rows[i].Cells[1].Text;
                    id += ",";
                }
            }
        }
        tid = tid.Substring(0, tid.Length - 1);
        Label1.Text = tid.ToString();
        id = id.Substring(0, id.Length - 1);
        InsertRecords(id, tid); // call method for insert and pass the StringCollection
    }

    public void InsertRecords(string id, string tid)
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite2\Allot.mdb");
        string sql = "insert into Table2 select * from Table1 where ID in(" + id + ");";
        string sql1 = "insert into Table2 (Quantity) values where ID in(" + tid + ");";
        conn.Open();
        OleDbCommand cmd1 = new OleDbCommand(sql1, conn);
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        cmd.ExecuteNonQuery();
        cmd1.ExecuteNonQuery();
    }
}

Edited by Nick Evan: Fixed formatting

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by esmail_merchant
0

Can you now help me

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Button1.Attributes.Add("onclick", "return confirm('Are you sure you want to select this units?');");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = string.Empty;
        string tid = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
        {
            TextBox tb = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1"); //find the TextBox
            if (tb != null)
            {
                if (tb.Enabled)
                {
                    tid += GridView1.Rows[i].Cells[1].Text;
                    tid += ",";
                }
            }
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
            if (cb != null)
            {
                if (cb.Checked)
                {
                    id += GridView1.Rows[i].Cells[1].Text;
                    id += ",";
                }
            }
        }
        tid = tid.Substring(0, tid.Length - 1);
        Label1.Text = tid.ToString();
        id = id.Substring(0, id.Length - 1);
        InsertRecords(id, tid); // call method for insert and pass the StringCollection
    }

    public void InsertRecords(string id, string tid)
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite2\Allot.mdb");
        string sql = "insert into Table2 select * from Table1 where ID in(" + id + ");";
        string sql1 = "insert into Table2 (Quantity) values where ID in(" + tid + ");";
        conn.Open();
        OleDbCommand cmd1 = new OleDbCommand(sql1, conn);
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        cmd.ExecuteNonQuery();
        cmd1.ExecuteNonQuery();
    }
}

Edited by mike_2000_17: Fixed formatting

0
TextBox tb = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1"); //find the TextBox
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox

i dont understand how come you are searching for both the controls in Cells[0]
i suppose Cell[0] should be Checkbox and Cell[1] should be TextBox..

i also need your markup code..

TextBox tb = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TextBox1"); //find the TextBox

try that and let me know the output.

0

Here is my code.
I have added checked checkbox value in other access database table.
Now I have added a textbox item template and i want to enter textbox data to same table.
How can I do it

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Button1.Attributes.Add("onclick", "return confirm('Are you sure you want to select this units?');");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = string.Empty;
        string tid = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
        {
            TextBox tb = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1"); //find the TextBox
            if (tb != null)
            {
                if (tb.Enabled)
                {
                    tid += GridView1.Rows[i].Cells[1].Text;
                    tid += ",";
                }
            }
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
            if (cb != null)
            {
                if (cb.Checked)
                {
                    id += GridView1.Rows[i].Cells[1].Text;
                    id += ",";
                }
            }
        }
        tid = tid.Substring(0, tid.Length - 1);
        Label1.Text = tid.ToString();
        id = id.Substring(0, id.Length - 1);
        InsertRecords(id, tid); // call method for insert and pass the StringCollection
    }

    public void InsertRecords(string id, string tid)
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite2\Allot.mdb");
        string sql = "insert into Table2 select * from Table1 where ID in(" + id + ");";
        string sql1 = "insert into Table2 (Quantity) values where ID in(" + tid + ");";
        conn.Open();
        OleDbCommand cmd1 = new OleDbCommand(sql1, conn);
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        cmd.ExecuteNonQuery();
        cmd1.ExecuteNonQuery();
    }
}
0

my friend when you debug wot did u get in

CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
TextBox tb = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1"); //find the TextBox

did u get the control and it's value..or u got some sort of exception..?

0

This is my original code from which I have inseted checked checkbox row from one acess database table to other.
Now I have taken a TextBox itemtemplate in that table and I want to insert text box data depending on checkbox checked in same database table.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = string.Empty;

        for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
        {

            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox

            if (cb != null)
            {

                if (cb.Checked)
                {

                    id += GridView1.Rows[i].Cells[1].Text;

                    id += ",";

                }

            }

        }

        id = id.Substring(0, id.Length - 1);

        InsertRecords(id); // call method for insert and pass the StringCollection
    }

    public void InsertRecords(string id)
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite1\Allot.mdb");
        string sql = "insert into Table2 select * from Table1 where ID in(" + id + ");";
        conn.Open();
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        cmd.ExecuteNonQuery();
    }

}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.