Hi,
I am very new to C#.I am trying to create a webform to save data in SQl server 2008 using c#.My problem is when i click on the save button, the code saves the 1st record every time instead of saving the new record.all my textboxes are showing the 1st record when i am trying to add new record.Below is my code for save button:
Please help me!!!
Shapna
namespace TestWebApp
{
public partial class About : System.Web.UI.Page
{
DataSet ds = new DataSet();
int MaxRows = 0;
int inc = 0;
SqlDataAdapter da;
protected void Page_Load(object sender, EventArgs e)
{
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["CnStr"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
con.Open();
da = new SqlDataAdapter("Select * from Av_details", con);
ds.Clear();
da.Fill(ds, "Av_details");
NavigateRecords();
MaxRows = ds.Tables[0].Rows.Count;
con.Close();
}
private void NavigateRecords()
{
DataRow dr;
dr = ds.Tables[0].Rows[inc];
TxtInvID.Text = dr.ItemArray.GetValue(0).ToString();
CBOInvType.SelectedItem.Text = dr.ItemArray.GetValue(1).ToString();
TxtItemName.Text = dr.ItemArray.GetValue(2).ToString();
TxtLocation.Text = dr.ItemArray.GetValue(3).ToString();
TxtMake.Text = dr.ItemArray.GetValue(4).ToString();
TxtModel.Text = dr.ItemArray.GetValue(5).ToString();
TxtSerial.Text = dr.ItemArray.GetValue(6).ToString();
}
protected void Cmd_Clear_Click(object sender, EventArgs e)
{
TxtInvID.Text = "";
CBOInvType.SelectedItem.Text = "Select Item Type";
TxtItemName.Text = "";
TxtLocation.Text = "";
TxtMake.Text = "";
TxtModel.Text = "";
TxtSerial.Text = "";
}
protected void Cmd_Add_Click(object sender, EventArgs e)
{
int maxID;
maxID = 0;
maxID = (int)ds.Tables["AV_Details"].Compute("Max(InventoryID)", "")+1;
TxtInvID.Text = maxID.ToString();
CBOInvType.SelectedItem.Text = "Select Item Type";
TxtItemName.Text = "";
TxtLocation.Text = "";
TxtMake.Text = "";
TxtModel.Text = "";
TxtSerial.Text = "";
}
protected void Cmd_Save_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
DataRow dr = ds.Tables[0].NewRow();
string ItemName = TxtItemName.Text.ToString();
dr[0] = TxtInvID.Text;
dr[1] = CBOInvType.SelectedItem.Text.ToString();
dr[2] = TxtItemName.Text.ToString();
dr[3] = TxtLocation.Text.ToString();
dr[4] = TxtMake.Text.ToString();
dr[5] = TxtModel.Text.ToString();
dr[6] = TxtSerial.Text.ToString();
dr[7] = "A";
ds.Tables[0].Rows.Add(dr);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
da.Update(ds, "AV_Details");
WebMsgBox.Show("Data Added Successfully!!");
}
}