Click here to Skip to main content
16,019,140 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code code giving above error on this line( DataGrid1.DataBind();)
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    SqlConnection con = new SqlConnection();
    string strPath = string.Empty;
    SqlCommand cmd = new SqlCommand();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
            con.Open();
            cmd = new SqlCommand("usp_subbrokerMapping", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 100000;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
            con.Close();
            DataGrid1.Visible = true;
            Label1.Visible = true;
            Label1.Text = "Data Displayed Successfully";
        }

    }
    protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        int i = 0;
        bool blnSelect = false;
        foreach (GridViewRow row in DataGrid1.Rows)
        {
            CheckBox rb = (CheckBox)row.FindControl("RowSelector");
            Label lbltagcode = (Label)row.FindControl("lbltagcode");
            Label lblParent = (Label)row.FindControl("lblParent");
            Label lblCategory_code = (Label)row.FindControl("lblCategory_code");


            if (rb.Checked)
            {
       
                 SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
                    con.Open();
                    cmd = new SqlCommand("usp_subbrokerMapping", con);
                    cmd.CommandType = CommandType.StoredProcedure;        
                    cmd.CommandTimeout = 100000;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                 cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataGrid1.DataSource = ds;
                    DataGrid1.DataBind();
                    con.Close();
                    DataGrid1.Visible = true;
                    Label1.Visible = true;
                    Label1.Text = "Data Inserted Successfully";
                 }

                rb.Checked = false;
                i++;        
               }
        }
       
       }
Posted
Updated 1-Aug-12 2:12am
v2
Comments
bbirajdar 1-Aug-12 8:12am    
Do not give your SQL server username - password in the open forums

Your buttonSave click event is updating data and not fetching it.
Hence the following code is not correct:
C#
SqlDataAdapter da = new SqlDataAdapter(cmd);
                 cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataGrid1.DataSource = ds;
                    DataGrid1.DataBind();

You need to rebind grid if you are fetching data again.
 
Share this answer
 
Comments
Sandeep Mewara 1-Aug-12 8:16am    
UPDATE: I see you have solved it and what looked like an issue was correct and now resolved.
Its Done
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    SqlConnection con = new SqlConnection();
    string strPath = string.Empty;
    SqlCommand cmd = new SqlCommand();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
            con.Open();
            cmd = new SqlCommand("usp_subbrokerMapping", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 100000;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
            con.Close();
            DataGrid1.Visible = true;
            Label1.Visible = true;
            Label1.Text = "Data Displayed Successfully";
        }

    }
    protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
        con.Open();
        int i = 0;
        bool blnSelect = false;
        foreach (GridViewRow row in DataGrid1.Rows)
        {
            CheckBox rb = (CheckBox)row.FindControl("RowSelector");
            Label lbltagcode = (Label)row.FindControl("lbltagcode");

            TextBox Rmcode = (TextBox)DataGrid1.FooterRow.FindControl("txtRmcode");
            if (rb.Checked)
            {
       
               
                    cmd = new SqlCommand("usp_subbrokerMapping", con);
                    cmd.CommandType = CommandType.StoredProcedure;        
                    cmd.CommandTimeout = 100000;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    cmd.Parameters.AddWithValue("@Sub_code", SqlDbType.VarChar).Value = lbltagcode.Text.ToString().Trim();
                    cmd.Parameters.AddWithValue("@rm_code", SqlDbType.VarChar).Value = Rmcode.Text.ToString();
                    cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
                    cmd.ExecuteNonQuery();            
                  
                    Label1.Visible = true;
                    Label1.Text = "Data Inserted Successfully";
                 }
                



                rb.Checked = false;
                i++;
                
               


            }
            con.Close();
        }
       
       }
 
Share this answer
 
v2

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