Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0 Gridview
hi am getting error while inserting records from gridview
in this line
 
 cmd.Parameters.Add("@Project", SqlDbType.VarChar).Value = ((DropDownList)GridView1.Rows[0].Cells[2].Controls[0]).SelectedItem.Text;
 
can anyone telme what is the issue in this line while saving records to sql??
 
thank you..
Posted 10-Dec-12 20:12pm
ythisbug2.1K
Comments
kkakadiya at 11-Dec-12 1:14am
   
try literal instead dropdownlist.
cmd.Parameters.Add("@Project", SqlDbType.VarChar).Value = ((Literal)GridView1.Rows[0].Cells[2].Controls[0]).SelectedItem.Text;
ythisbug at 11-Dec-12 1:26am
   
thank you..
am getting error..
literal does not contain a defination for selecteditem or no extension method..??
kkakadiya at 11-Dec-12 1:28am
   
can u please paste your code here? so here i can check also....
thanks,.
ythisbug at 15-Dec-12 0:40am
   
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO tblTimeSheet(tsProject,tsStartDate,tsEndDate,tsMonday,tsTuesday,tsWednesday,tsThursday,tsFriday,tsSaturday) VALUES(@Project,@StartDate,@EndDate,@Monday,@Tuesday,@Wednesday,@Thursday,@Friday,@Saturday)";
cmd.Parameters.Add("@Project", SqlDbType.VarChar).Value = ((DropDownList)GridView1.Rows[0].Cells[2].Controls[0]).SelectedItem.Text;
 

//cmd.Parameters.Add("@Project", SqlDbType.VarChar).Value = ((DropDownList)GridView1.Rows[0].Cells[2].FindControl("ddlProject"));
cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text;
cmd.Parameters.Add("@Monday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[5].Controls[0]).Text;
cmd.Parameters.Add("@Tuesday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[6].Controls[0]).Text;
cmd.Parameters.Add("@Wednesday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[7].Controls[0]).Text;
cmd.Parameters.Add("@Thursday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[8].Controls[0]).Text;
cmd.Parameters.Add("@Friday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[9].Controls[0]).Text;
cmd.Parameters.Add("@Saturday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[10].Controls[0]).Text;
 


cmd.Connection = con;
 
con.Open();
 
cmd.ExecuteNonQuery();
 
con.Close();
 
}
 

else
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE tblTimeSheet SET tsProject=@Project,tsStartDate=@StartDate,tsEndDate=@EndDate,tsMonday=@Monday,tsTuesday=@Tuesday,tsWednesday=@Wednesday,tsThursday=@Thursday,tsFriday=@Friday,tsSaturday=@Saturday WHERE tsID=@ID";

 
cmd.Parameters.Add("@Project", SqlDbType.VarChar).Value = ((DropDownList)GridView1.Rows[0].Cells[2].Controls[0]).Text;
cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text;
cmd.Parameters.Add("@Monday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[5].Controls[0]).Text;
cmd.Parameters.Add("@Tuesday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[6].Controls[0]).Text;
cmd.Parameters.Add("@Wednesday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[7].Controls[0]).Text;
cmd.Parameters.Add("@Thursday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[8].Controls[0]).Text;
cmd.Parameters.Add("@Friday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[9].Controls[0]).Text;
cmd.Parameters.Add("@Saturday", SqlDbType.Decimal).Value = ((TextBox)GridView1.Rows[0].Cells[10].Controls[0]).Text;
 
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
cmd.Connection = con;
 
con.Open();
 
cmd.ExecuteNonQuery();
 
con.Close();
 

 
}
 

 
GridView1.EditIndex = -1;
 
BindData();

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150302.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100