Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 UPDATE a field in DB using result of other query?

Author  Topic 

imnitsy
Starting Member

1 Post

Posted - 2011-06-08 : 15:40:23
Hello,

I am a beginner to SQl, well all i need to do now is to update the RegistrationId feild of my database by selecting the row with the help of a primary keyed field.

The code i used is as follows, it is showing the error the :
Error 68 The name 'str' does not exist in the current context

The Code i used is as follows:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class InterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();

con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
con.Open();
cmd.CommandText = "SELECT * FROM StudentDatabase";
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(ds, "register");

DataTable dataTable = ds.Tables["register"];
int currRec = 0;
int totalRec = dataTable.Rows.Count;
int totalCol = dataTable.Columns.Count;
currRec = totalRec - 1;

TextBox1.Text = dataTable.Rows[currRec]["StudentId"].ToString() + dataTable.Rows[currRec]["SerialNo"].ToString();
String str = dataTable.Rows[currRec]["SerialNo"].ToString();

con.Close();
}

catch (Exception ex)
{
Console.WriteLine("Exception", ex);
}

}
private void FillControls()
{
throw new Exception("The method or operation is not implemented.");
}

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();

conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True";
conn.Open();

try
{
SqlCommand cmdd = new SqlCommand("UPDATE StudentDatabase SET RegistrationId = "+ TextBox1.Text.Trim() +" WHERE SerialNo =" + str );
//cmdd.Parameters.AddWithValue("@RegistrationId", SqlDbType.NVarChar).Value = TextBox1.Text;
cmdd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Exception", ex);
}
conn.Close();
Response.Redirect("final.aspx");

}
}
   

- Advertisement -