Click here to Skip to main content
15,884,948 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day,

I have created a web form for sumbmiting data to the database and it is working with no errors. It diplays on a griedview

now i have decided to add an update button. but i dont know where to start with my updating satatement. can you please help me on how to write an update statement.

I want to know if it is possible for me to click data in a griedview and it shows on the form so that i can update,is yes? can you please show me how?

You can rectify me if theres mistake i did.

I think now i have provided enough details.

Thanks.....



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

public partial class Register : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);
    
        
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        conn.Open();
        
        
    }

    protected void btnregister_Click(object sender, EventArgs e)
    {
        SqlCommand comm = new SqlCommand("insert into tblreg values('"+txtuser.Text+"','"+txtPass.Text+"','"+txtFulln.Text+"','"+txtSurname.Text+"','"+txtemail.Text+"','"+dropCountry.Text+"','"+dropProvince.Text+"')",conn);
        comm.ExecuteNonQuery();
        GridView1.DataBind();
        txtuser.Text = "";
        txtPass.Text = "";
        txtFulln.Text = "";
        txtSurname.Text = "";
        txtemail.Text = "";
        dropCountry.SelectedItem.ToString();
        dropProvince.SelectedItem.ToString();
        

        //try
        //{
        //    conn.Close();
        //    Response.Redirect("Login.aspx");
        //}
        //catch(Exception ex)
        //{
        //    Response.Write("Error");
        //}
        //finally
        //{
        //    Response.Write("Executed");
        //}
    }

    private void formloaddata()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);
        string strquery = "Select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province";
        SqlDataReader read = new SqlDataReader(conn, strquery);
        GridView1.DataBind();


    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
       
        
    }
}
Posted
Updated 14-May-13 4:16am
v2
Comments
Johnny J. 14-May-13 9:34am    
What have you got so far? Where are you stuck? You need to give us a more specific question. We don't write full applications for you...
Member 10010198 14-May-13 9:36am    
this is what i was traying to write.

private void formloaddata()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);
string strquery = "Select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province";
SqlDataReader read = new SqlDataReader(conn, strquery);
GridView1.DataBind();


}
[no name] 14-May-13 9:54am    
You never open the connection and you never execute the query.
Member 10010198 14-May-13 9:56am    
this is my coding so far......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

public partial class Register : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);


protected void Page_Load(object sender, EventArgs e)
{


conn.Open();


}

protected void btnregister_Click(object sender, EventArgs e)
{
SqlCommand comm = new SqlCommand("insert into tblreg values('"+txtuser.Text+"','"+txtPass.Text+"','"+txtFulln.Text+"','"+txtSurname.Text+"','"+txtemail.Text+"','"+dropCountry.Text+"','"+dropProvince.Text+"')",conn);
comm.ExecuteNonQuery();
GridView1.DataBind();
txtuser.Text = "";
txtPass.Text = "";
txtFulln.Text = "";
txtSurname.Text = "";
txtemail.Text = "";
dropCountry.SelectedItem.ToString();
dropProvince.SelectedItem.ToString();


//try
//{
// conn.Close();
// Response.Redirect("Login.aspx");
//}
//catch(Exception ex)
//{
// Response.Write("Error");
//}
//finally
//{
// Response.Write("Executed");
//}
}

private void formloaddata()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);
string strquery = "Select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province";
SqlDataReader read = new SqlDataReader(conn, strquery);
GridView1.DataBind();


}
[no name] 14-May-13 10:02am    
Yes... and? As I said, you never open the connection and you never execute the query. And you need to use the Improve question widget to post your code and describe your problem.

I would suggest you to start here: ADO.NET[^]

There you'll find code examples for ADO.NET: http://msdn.microsoft.com/en-us/library/dw70f090.aspx[^]

From CP Knowledge Base:
Using ADO.NET for beginners[^]
A Beginner's Tutorial for Understanding ADO.NET[^]
 
Share this answer
 
C#
private void formloaddata()
    {
 SqlDataReader read
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);
conn.open   //Open your Connection
        string strquery = "Select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province from "Write your TableName"";
        SqlCommand cmd=new SqlCommand(strquery,conn);
        read = cmd.ExecutenonQuery();
if read.hasrows
{
read.Read();
textbox1.text=read("UserID"); // assign this reader to your respective TextBoxes
textbox2.text=read("Username");
textbox3.text=read("Password");
textbox4.text=read("Fullname");
textbox5.text=read("Surname");
textbox6.text=read("Email ");
textbox7.text=read("Address");
textbox8.text=read("Country");
textbox9.text=read("Province");
}     

    }


write the code in a try catch block so if error occurs you will come to know what the error is
 
Share this answer
 
Comments
Member 10010198 15-May-13 4:56am    
private void formloaddata()
{
SqlDataReader read;
SqlCommand comm = new SqlCommand(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);
conn.Open();
string strquery = "select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province from tblreg";
SqlCommand comd = new SqlCommand(strquery, conn);

read = comd.ExecuteNonQuery(); Error//Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'

if(read.HasRows)
{
while (read.Read())
{

//Errors for code below 'read' is a 'variable' but is used like a 'method'
txtuser.Text = read("Username"); Error//
txtPass.Text = read("Password");
txtFulln.Text = read("Fullname");
txtSurname.Text = read("Surname");
txtemail.Text = read("Email Address");
dropCountry.Text = read("Country");
dropProvince.Text = read("province");

}

}
// is my try catch statement ok?
try
{
conn.Close();

}
catch (Exception ex)
{
Response.Write("Error");
}
finally
{
Response.Write("Executed");
}
Member 10010198 15-May-13 4:57am    
i got those 2 erros that i have commented........Thanks

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