为什么数据没有成功 [英] Why data is not featching
本文介绍了为什么数据没有成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么最初不从数据库获取数据以按照期望更新新值.
我正在使用会话值从数据库中获取数据,如下所示.
我的CS代码在下面给出
Why data from database is not fetching initially to update new value as per desire.
I am using session value to fetch from database as per given below.
my cs code is given below
if (Page.IsValid == true)
{
string sc1 = "";
if (rbStudent.Checked == true)
{
sc1 = "Student";
}
if (rbPFaculty.Checked == true)
{
sc1 = "Private Faculty";
}
if (rbSFaculty.Checked == true)
{
sc1 = "School Faculty";
}
SqlConnection CON = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["tuitionConnectionString"].ConnectionString);
SqlTransaction tr = null;
CON.Open();
tr = CON.BeginTransaction();
ds = new DataSet();
string sql = "select * from registration";
da = new SqlDataAdapter(sql, CON);
da.Fill(ds, "registration");
//string sa = txtSess.Text;
//txtFname.Text = sa;
if (Session["name"] != null)
{
string sa = Session["name"].ToString();
int r = 0;
DataRow[] rs;
rs = ds.Tables["registration"].Select("email='" + sa + "'");
r = rs.Length;
if (r > 0)
{
//FETCH DATA FROM DATABASE
DataRow dr;
dr = rs[0];
txtFname.Text = dr["fname"].ToString();
txtLname.Text = dr["lname"].ToString();
txtDob.Text = dr["dob"].ToString();
txtCno.Text = dr["cont"].ToString();
sc1 = dr["rtype"].ToString();
ddClass.SelectedItem.Text = dr["tclass"].ToString();
txtSchool.Text = dr["school"].ToString();
ddBoard.SelectedItem.Text = dr["board"].ToString();
ddCity.SelectedItem.Text = dr["city"].ToString();
txtPCode.Text = dr["pcode"].ToString();
ddState.SelectedItem.Text = dr["state"].ToString();
try
{
//UPDATE RECORDS in DataBase
com = CON.CreateCommand();
com.CommandText = "UPDATE registration SET fname='" + txtFname.Text + "',lname='" + txtLname.Text + "',dob='" + txtDob.Text + "',cont='" + txtCno.Text + "',rtype='" + sc1.ToString() + "',tclass='" + ddClass.SelectedItem.Text + "',school='" + txtSchool.Text + "',board='" + ddBoard.SelectedItem.Text + "',city='" + ddCity.SelectedItem.Text + "',pcode='" + txtPCode.Text + "',state='" + ddState.SelectedItem.Text + "' WHERE email='" + txtSess.Text + "'";
try
{
com.ExecuteNonQuery();
System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
lbl1.ForeColor = System.Drawing.Color.Yellow;
lbl1.BackColor = System.Drawing.Color.Blue;
lbl1.Text = "Your record UPDATED sucessfully";
ph1.Controls.Add(lbl1);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
catch (Exception ex)
{
Response.Write("" + ex);
tr.Rollback();
}
finally
{
CON.Close();
}
}
}
}
推荐答案
我只是在猜测,但是您在第一次运行时会遇到异常吗?我打赌你会这么做.
Page.IsValid仅在首先调用验证例程时才有效-如果在此之前尝试使用它,则会收到错误消息并停止执行.因此您的数据库将不会被加载.如果您忽略了我们看不到的try-catch错误,那可能就是您的问题.
I''m just guessing, but do you get an exception on the first run? I''m betting you do.
Page.IsValid is only valid when the validation routine has been called first - if you try to use it before that, you will get an error and your execution will stop. So your database will not be loaded. If you are ignoring errors with a try-catch we can''t see, then that could be your problem.
这篇关于为什么数据没有成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文