Problem with GridView

Jan 5, 2011
Reaction score

I have a problem with dipslaying the GridView. If I use the code as it is now( the code below) I can not see the inserted data in the GridView which was empty before inserting, neither the data in the GridView if the source/table is not empty while loading the page. I have an empty GridView, and I can see the inserted data only in Server Explorer. Here is the code:

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
SqlConnection liniiConnection = new SqlConnection();
DataSet liniiDataSet = new DataSet();
SqlDataAdapter liniiSqlDataAdapter = new SqlDataAdapter();

liniiConnection.ConnectionString = SqlDataSource1.ConnectionString;
liniiSqlDataAdapter.SelectCommand = new
SqlCommand(SqlDataSource1.SelectCommand, liniiConnection);

if (!IsPostBack)
if (liniiDataSet.Tables[0].Rows.Count == 0)
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("LinijaID", typeof(int)));
dt.Columns.Add(new DataColumn("Ime", typeof(string)));

DataRow dr = dt.NewRow();
dr["LinijaID"] = Convert.ToInt32(txtLinijaId.Text);
dr["Ime"] = "MZT";
GridView1.DataSource = dt;
ViewState.Add("key", "1");
ViewState.Add("key", "0");
GridView1.DataSource = SqlDataSource1;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (ViewState["key"].ToString() == "1")
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)

DropDownList ddl = new DropDownList();

ddl.ID = row.RowIndex.ToString();
for (Int32 i = 0; i < 5; i++)
ddl.Items.Add(new ListItem(i.ToString(), "0", true));

protected void Button1_Click(object sender, EventArgs e)

TextBox LinijaId = txtLinijaId as TextBox;

//TextBox LinijaId = GridView1.FooterRow.FindControl("LinijaIdTextBox") as TextBox;

DropDownList Ime = GridView1.FooterRow.FindControl("ImeDropDownList") as DropDownList;

SqlDataSource1.InsertParameters["LinijaId"].DefaultValue = LinijaId.Text;
//SqlDataSource1.InsertParameters["LinijaId"].DefaultValue = txtLinijaId.Text;

SqlDataSource1.InsertParameters["Ime"].DefaultValue = Ime.SelectedValue;



<head id="Head1" runat="server">

<%--<Untitled Page</title>--%>



<form id="form1" runat="server">


<asp:TextBox ID="txtLinijaId" runat="server" CausesValidation="True"
<asp:Label ID="Label7" runat="server" Text="Ime na linija"></asp:Label>
<br />
<asp:TextBox ID="txtLineList" runat="server"></asp:TextBox>
<asp:Label ID="Label8" runat="server" Text="Lista"></asp:Label>
<br />
<asp:TextBox ID="txtGeoVal" runat="server"></asp:TextBox>
<asp:Label ID="Label9" runat="server" Text="GeoVal"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource4" runat="server" InsertCommand="INSERT Linii ( LinijaIme, LineList, GeoVal)
VALUES ( @Ime, @LineList, @GeoVal)">
<asp:ControlParameter ControlID="txtLinijaId" Name="Ime" PropertyName="Text" />
<asp:ControlParameter ControlID="txtLineList" Name="LineList"
PropertyName="Text" />
<asp:ControlParameter ControlID="txtGeoVal" Name="GeoVal" PropertyName="Text" />
<asp:ValidationSummary ID="textBoxValidationSummary1" runat="server"
HeaderText="Ovie greski se najdeni pri vnesot" />

<br />

<asp:Label ID="Label5" runat="server" Text="Vkupno postojki na ovaa linija:"></asp:Label>

<asp:Label ID="Label6" runat="server"></asp:Label>
<br />
<br />

<asp:GridView ID="GridView1" Runat="server" DataKeyNames="LinijaID"

AutoGenerateColumns="False" ShowFooter="True" CellSpacing="1" AllowPaging="True"
onrowcreated="GridView1_RowCreated" >


<asp:TemplateField HeaderText="LinijaId">


<asp:Label ID="LinijaIDLabel" Runat="Server"><%# Eval("LinijaID") %></asp:Label>



<asp:TextBox ID="LinijaIDTextBox" Runat="server"></asp:TextBox>



<asp:TemplateField HeaderText="PIme">


<asp:DropDownList ID="ImeDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="Ime" DataValueField="Ime">


<asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [Ime] FROM [Postojki]"

ConnectionString="<%$ ConnectionStrings:ConnString%>">


<asp:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" />



<asp:DropDownList ID="ImeDropDown" SelectedValue='<%# Bind("Ime") %>' Runat="Server" DataSourceID="SqlDataSource3" DataTextField="Ime" DataValueField="Ime" ></asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [Ime] FROM [Postojki]"

ConnectionString="<%$ ConnectionStrings:ConnString%>" EnableCaching="True">






<asp:SqlDataSource ID="SqlDataSource1" Runat="server"

InsertCommand="INSERT INTO [LinPost2] ([LinijaID], [Ime]) VALUES (@LinijaID, @Ime)"

SelectCommand="declare @LinijaID int
SELECT [LinijaID], [Ime] FROM [LinPost2] where ([email protected]) ORDER BY [LinijaID] ASC"

ConnectionString="<%$ ConnectionStrings:ConnString %>">

<asp:ControlParameter ControlID="txtLinijaId" DefaultValue="" Name="@LinijaID"
PropertyName="Text" />
<asp:parameter Name="LinijaID" />


<asp:parameter Type="String" Name="LinijaID"></asp:parameter>



<asp:parameter Type="String" Name="LinijaID"></asp:parameter>

<asp:parameter Type="String" Name="Ime"></asp:parameter>



<asp:ControlParameter ControlID="txtLinijaId" Name="LinijaID"
PropertyName="Text" Type="String" />

<asp:parameter Type="String" Name="Ime"></asp:parameter>







And if I don't write the page_load and GridView1_RowDataBound events, I have the GridView with all data displayed as well as the data I am inserting, but I can't see the GridView when it is empty, when the table/data source is empty while loading the page.

Could anyone help me please?
I am eagerly waiting for your respones. Thank you!


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question