grid view not accepting value on single click of button

Posted by Ankitsrist under ASP.NET on 11/26/2012 | Points: 10 | Views : 1503 | Status : [Member] | Replies : 3
i have one textbox and gridview....the value which i enter into textbox first store into database and then it displays into the grid view through database...but i have to click twice to display value in gridview why m getting this problem.....my code is as below


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;
public partial class statemaster : System.Web.UI.Page
{
dbClass myobj = new dbClass();
public SqlConnection sqlcon = new SqlConnection();
string mystr;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
fillgrid();
}

}
public void fillgrid()
{
myobj.myconnection();
string sqlQuery = ("select ID, State_name from state");
SqlDataAdapter sqlda = new SqlDataAdapter(sqlQuery, myobj.sqlcon);
DataSet myds = new DataSet();
sqlda.Fill(myds, "table0");
GridView1.DataSource = myds;
GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
myobj.myconnection();
SqlCommand sqlcmd = new SqlCommand("Insert into state(State_name) values ('" + TextBox1.Text + "')", myobj.sqlcon);
sqlcmd.ExecuteNonQuery();


fillgrid();
GridView1.DataBind();





}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

myobj.myconnection();
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["ID"].ToString());
//string State_name = GridView1.DataKeys[e.RowIndex].Values["State_name"].ToString();

SqlCommand cmd = new SqlCommand("delete from state where ID="+ID, myobj.sqlcon);

cmd.ExecuteNonQuery();


fillgrid();
GridView1.DataBind();

}


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

myobj.myconnection();
mystr = "select * from state where id = '" + GridView1.SelectedValue .ToString () + "'";
SqlCommand cmd = new SqlCommand(mystr, myobj.sqlcon);
SqlDataReader sqldr = cmd.ExecuteReader();
sqldr.Read ();
if (sqldr.HasRows)
{
TextBox1.Text = sqldr["State_name"].ToString();


}
else
{
TextBox1.Text = "";

}

}
}





Responses

Posted by: Krv on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Can you check and tell me whether you are getting the updated result in the myds dataset, during the button click event

GridView1.DataSource = myds;


Are you using any ajax on the aspx page

Thanks,
R.Vasanth
MCTS(SQL Server & BI)
http://bigdatatech.blogspot.com

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Once see below code, It just Inserting a record and after inserting the gridview will be dispalyed after buttonClick. DO modification as per your requirement
web.config:
<?xml version="1.0"?>

<configuration>
<connectionStrings>
<add name="ConnStr" connectionString="Data Source=SR-PC\SQLEXPRESS;Initial Catalog=Personal Deatils;Persist Security Info=True;User ID=rajeshwar;Password=savitha24;" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>

aspx:
 <div>

<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
FirstName:<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
LastName<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
Age<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Insert" OnClick="InsertData_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>

CodeBehind:

 string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillGrid();
}

}

private void fillGrid()
{
DataSet dset = new DataSet();
using (SqlConnection conn = new SqlConnection(_connStr))
{

string sql = "SELECT AutoId, FirstName, LastName, Age FROM Table_1 Order By AutoID ASC";

using (SqlCommand cmd = new SqlCommand(sql, conn))
{

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{

ad.Fill(dset);
}

}

}

GridView1.DataSource = dset.Tables[0];

GridView1.DataBind();

}
protected void InsertData_Click(object sender, EventArgs e)
{

try
{
int returnValue = 0;
//get the connection
using (SqlConnection conn = new SqlConnection(_connStr))
{

string sql = "INSERT INTO Table_1 (FirstName,LastName,Age) VALUES" + "(@FirstName, @LastName,@Age)";

using (SqlCommand cmd = new SqlCommand(sql, conn))
{

SqlParameter[] prms = new SqlParameter[3];
prms[0] = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
prms[0].Value = txtFirstName.Text.Trim();
prms[1] = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
prms[1].Value = txtLastName.Text.Trim();
prms[2] = new SqlParameter("@Age", SqlDbType.Int);
prms[2].Value = int.Parse(txtAge.Text.Trim());

cmd.Parameters.AddRange(prms);
conn.Open();
returnValue = cmd.ExecuteNonQuery();
conn.Close();
}
fillGrid();
}
if (!returnValue.Equals(0))
{
lblMessage.Text = " Records inserted successfully !";
}
txtFirstName.Text = "";
txtLastName.Text = "";
txtAge.Text = "";
}
catch (Exception ee)
{
lblMessage.Text = "Please Enter all Fields to insert a record";
}

}


Thanks & Regards

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 11/29/2012 [Member] Starter | Points: 25

Up
0
Down
no krv m not using ajax

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response