SqlConnection con = new SqlConnection(connectionstring);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select patientName,patientNRIC from patientInfo where patientNRIC='" + searchPatNric + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
Thiv
Member
5 Points
45 Posts
GridView not updating
Jan 06, 2013 01:24 PM|LINK
Hi all my gridview is not updating. I hav googled the forums and tried the solutions but still facing errors. Below are my codes.
<asp:GridView ID="GridView1" runat="server" Width = "100px" HeaderStyle-HorizontalAlign="Justify"
onrowediting="EditPatient" onrowupdating="UpdatePatient" onrowcancelingedit="CancelEdit" DataKeyNames="patientNRIC">
<Columns>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left"
ControlStyle-Width="50px">
<EditItemTemplate>
<asp:TextBox ID="txtPatName" runat="server" Text='<%#Bind("patientName") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<%# Eval("patientName")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nric" HeaderStyle-HorizontalAlign="Left"
ControlStyle-Width="80px">
<ItemTemplate>
<asp:Label ID="lblPatNric" runat="server"
Text='<%# Eval("patientNRIC")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" OnClientClick="return confirm('Update?')"
ValidationGroup="Update"></asp:LinkButton>
<asp:ValidationSummary ID="vsUpdate" runat="server" ShowMessageBox="true" ShowSummary="false"
ValidationGroup="Update" Enabled="true" HeaderText="Validation Summary..." />
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField></Columns></asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData(xxx);
}
private void BindData(String searchPatNric)
{
SqlConnection con = new SqlConnection(connectionstring);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select patientName,patientNRIC from patientInfo where patientNRIC='" + searchPatNric + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
protected void UpdatePatient(object sender, GridViewUpdateEventArgs e)
{
string nRic = GlobalVariables.nric;
string nameOfPat = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtPatName")).Text;
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update patientInfo set patientName=@patientName where patientNRIC=" + nRic;
cmd.Parameters.Add("@patientName", SqlDbType.VarChar).Value = nameOfPat;
GridView1.EditIndex = -1;
BindData(nRic);
}
}
Help is really appreciated :)
sarathi125
Star
13599 Points
2691 Posts
Re: GridView not updating
Jan 06, 2013 01:56 PM|LINK
Hi,
I think you not executing the Query since you adding all parameters to it.
Are you getting any errors? If so then post here.
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
Thiv
Member
5 Points
45 Posts
Re: GridView not updating
Jan 06, 2013 02:24 PM|LINK
Hi. i am not getting any errors.
sarathi125
Star
13599 Points
2691 Posts
Re: GridView not updating
Jan 06, 2013 02:31 PM|LINK
Hi,
Execute the query like
cmd.ExecuteNonQuery();
// detach the SqlParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
AL MUBARAK
Member
164 Points
115 Posts
Re: GridView not updating
Jan 06, 2013 02:44 PM|LINK
Dear Thiv,
You are facing any issue with Grid View, or you are facing any issue with Record Update.
For update :
Use Cmd.ExecuteNonQuery() to update your parameterts to corresponding tables.
NOTE : While the time of Update, requeired Open and close connection.
Al Mubarak
Thiv
Member
5 Points
45 Posts
Re: GridView not updating
Jan 06, 2013 03:01 PM|LINK
Hi this is what I have edited my update gridview codes to. I suppose I am facing an issue with update. But I am facing this error now:
ExecuteNonQuery: Connection property has not been initialized.
Codes
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update patientInfo set patientName=@patientName where patientNRIC=" + nRic;
cmd.Parameters.AddWithValue("@patientName", nameOfPat);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Parameters.Clear();
sarathi125
Star
13599 Points
2691 Posts
Re: GridView not updating
Jan 06, 2013 03:06 PM|LINK
Hi,
You need to assign the connection to the sql command
Try like the following
SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "update patientInfo set patientName=@patientName where patientNRIC=" + nRic; cmd.Parameters.AddWithValue("@patientName", nameOfPat); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); cmd.Parameters.Clear();Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
Thiv
Member
5 Points
45 Posts
Re: GridView not updating
Jan 07, 2013 01:52 AM|LINK
yes it works with the below modifications
cmd.CommandText = "update patientInfo set patientName=@patientName where patientNRIC=@patientNRIC";
cmd.Parameters.Add("@patientName", SqlDbType.VarChar).Value = nameOfPat;
cmd.Parameters.Add("@patientNRIC", SqlDbType.VarChar).Value = nRic;
sarathi125
Star
13599 Points
2691 Posts
Re: GridView not updating
Jan 07, 2013 03:27 AM|LINK
Hi,
If its working fine, then mark the post as answer
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets