Click here to Skip to main content
15,888,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI all!!

I have a gridview on whose rowcommand i am making a panel visible and the from that panel i can update the data of that row of gridview.
I have used data reader to retreive values in the textbox inside the panel. User changes the values of the textboxes.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Change")
    {
        int id = Convert.ToInt32(e.CommandArgument.ToString());
        pnl1.Visible = true;
        con.Open();
        SqlCommand cmd = new SqlCommand("Select SName,SAge,SClass,SEmail from Student where ID='" + id + "' ", con);
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        txtEditAge.Text = dr.GetValue(1).ToString();
        txtEditName.Text = dr.GetValue(0).ToString();
        txtEditClass.Text = dr.GetValue(2).ToString();
        txtEditEmail.Text = dr.GetValue(3).ToString();
        HiddenField1.Value = id.ToString() ;

    }
}


now i want to do this using datatable

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "Change")
       {
           int id = Convert.ToInt32(e.CommandArgument.ToString());
           pnl1.Visible = true;
           con.Open();
           SqlCommand cmd = new SqlCommand("Select SName,SAge,SClass,SEmail from Student where ID='" + id + "' ", con);
           cmd.ExecuteNonQuery();
           DataTable dt = new DataTable();
           DataRow dr;

           dt.Columns.Add("Name");
           dt.Columns.Add("Age", System.Type.GetType("System.Int32"));
           dt.Columns.Add("Class");
           dt.Columns.Add("Email");
           dr = dt.NewRow();
           dr[0] =  ????????????????????????


       }

i want SName at dr[0] , SAge at dr[1]... Please tell me how to do it........
Posted

 
Share this answer
 
Comments
ujjwal uniyal 29-Feb-12 3:47am    
Sir , the link u gave me adds the value to data-table by coding. i don't have the luxuary of adding it myself insted the datatable should be filled on the rowcommand of gridview. Can u please suggest me what should i do ???
Espen Harlinn 29-Feb-12 14:54pm    
5'ed!
Sergey Alexandrovich Kryukov 1-Mar-12 15:42pm    
Sure, a 5.
--SA
ProEnggSoft 27-Mar-12 9:14am    
+5
Here is a nice list of tutorials:
Data Access: The Official Microsoft ASP.NET Site[^]

It's one of the better sources for learning what you need.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Wonde Tadesse 29-Feb-12 21:28pm    
5+
Espen Harlinn 1-Mar-12 4:07am    
Thank you, Wonde!
thatraja 1-Mar-12 9:41am    
Right, 5!
Espen Harlinn 1-Mar-12 14:49pm    
Thank you, thatraja :-D
Sergey Alexandrovich Kryukov 1-Mar-12 15:42pm    
That's it, huh? My 5.
--SA
Hope it will help you

string queryString =    "SELECT CustomerID, CompanyName FROM dbo.Customers"; SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);  DataSet customers = new DataSet(); 
adapter.Fill(customers, "Customers");
 
Share this answer
 
Comments
ujjwal uniyal 29-Feb-12 3:29am    
it's not what i am looking for but thanks for the answer
you wants to get value form the data table?? if it is yes then you can use below code

C#
dr[0] = dt.Rows[0][0].toString();
dr[1] = dt.Rows[0][1].toString();


in the DT.Rows["Row Index"]["Column index"], hope it is helpful to you
 
Share this answer
 
Comments
ujjwal uniyal 29-Feb-12 3:44am    
no i want to add values in the datatable.
Use following code to insert recordset to DataTable,

C#
DataTable dataTable = new DataTable();
SqlCommand objCmd= new SqlCommand("Select SName,SAge,SClass,SEmail from Student where ID='" + id + "' ", con);
SqlDataReader dr = objCmd.ExecuteReader();
dataTable.Load(dr);
 
Share this answer
 
Comments
ujjwal uniyal 29-Feb-12 4:37am    
hmm it worked but the situation given to me is that i can not use data reader....
Career Web Helper 29-Feb-12 5:35am    
ExecuteNonQuery() returns no. of rows affected,so u can also try with DataAdapter e.g.SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCmd);objDataAdapter.Fill(dataTable);
Hi there..

Try this..

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "Change")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            pnl1.Visible = true;
            DataRow[] rows = YourDataTable.Select(" AutoID = "+id);
            //Now fetch the values from the datarow..
            //Your code goes here..
        }
}

try this.. all the best..
 
Share this answer
 

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